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 long 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 ) {
529 c->c_conn_state = SLAP_C_CLIENT;
530 c->c_struct_state = SLAP_C_USED;
531 c->c_close_reason = "?"; /* should never be needed */
532 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
533 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
538 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
539 ber_str2bv( peername, 0, 1, &c->c_peer_name );
541 c->c_n_ops_received = 0;
542 c->c_n_ops_executing = 0;
543 c->c_n_ops_pending = 0;
544 c->c_n_ops_completed = 0;
550 /* set to zero until bind, implies LDAP_VERSION3 */
553 #ifndef SLAPD_MONITOR
554 if ( global_idletimeout > 0 )
555 #endif /* ! SLAPD_MONITOR */
557 c->c_activitytime = c->c_starttime = slap_get_time();
560 #ifdef LDAP_CONNECTIONLESS
562 if( flags == CONN_IS_UDP ) {
565 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
566 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
568 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
569 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
570 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
571 LBER_SBIOD_LEVEL_PROVIDER, NULL );
576 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
577 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
579 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
580 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
584 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
585 INT_MAX, (void*)"ldap_" );
588 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
589 c /* non-NULL */ ) < 0 )
591 Debug( LDAP_DEBUG_ANY,
592 "connection_init(%d, %s): set nonblocking failed\n",
593 s, c->c_peer_name.bv_val, 0 );
596 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
597 id = c->c_connid = conn_nextid++;
598 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
600 c->c_conn_state = SLAP_C_INACTIVE;
601 c->c_struct_state = SLAP_C_USED;
602 c->c_close_reason = "?"; /* should never be needed */
604 c->c_ssf = c->c_transport_ssf = ssf;
608 if ( flags == CONN_IS_TLS ) {
610 c->c_needs_tls_accept = 1;
613 c->c_needs_tls_accept = 0;
617 slap_sasl_open( c, 0 );
618 slap_sasl_external( c, ssf, authid );
620 slapd_add_internal( s, 1 );
621 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
623 backend_connection_init(c);
628 void connection2anonymous( Connection *c )
630 assert( connections != NULL );
634 ber_len_t max = sockbuf_max_incoming;
635 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
638 if ( !BER_BVISNULL( &c->c_authmech ) ) {
639 ch_free(c->c_authmech.bv_val);
641 BER_BVZERO( &c->c_authmech );
643 if ( !BER_BVISNULL( &c->c_dn ) ) {
644 ch_free(c->c_dn.bv_val);
646 BER_BVZERO( &c->c_dn );
648 if ( !BER_BVISNULL( &c->c_ndn ) ) {
649 ch_free(c->c_ndn.bv_val);
651 BER_BVZERO( &c->c_ndn );
653 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
654 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
656 BER_BVZERO( &c->c_sasl_authz_dn );
658 c->c_authz_backend = NULL;
662 connection_destroy( Connection *c )
665 unsigned long connid;
666 const char *close_reason;
669 assert( connections != NULL );
671 assert( c->c_struct_state != SLAP_C_UNUSED );
672 assert( c->c_conn_state != SLAP_C_INVALID );
673 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
674 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
676 assert( c->c_txn == CONN_TXN_INACTIVE );
677 assert( c->c_txn_backend == NULL );
678 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
680 assert( c->c_writewaiter == 0);
682 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
683 connid = c->c_connid;
684 close_reason = c->c_close_reason;
686 ldap_pvt_thread_mutex_lock( &connections_mutex );
687 c->c_struct_state = SLAP_C_PENDING;
688 ldap_pvt_thread_mutex_unlock( &connections_mutex );
690 backend_connection_destroy(c);
695 c->c_activitytime = c->c_starttime = 0;
697 connection2anonymous( c );
698 c->c_listener = NULL;
700 if(c->c_peer_domain.bv_val != NULL) {
701 free(c->c_peer_domain.bv_val);
703 BER_BVZERO( &c->c_peer_domain );
704 if(c->c_peer_name.bv_val != NULL) {
705 free(c->c_peer_name.bv_val);
707 BER_BVZERO( &c->c_peer_name );
709 c->c_sasl_bind_in_progress = 0;
710 if(c->c_sasl_bind_mech.bv_val != NULL) {
711 free(c->c_sasl_bind_mech.bv_val);
713 BER_BVZERO( &c->c_sasl_bind_mech );
715 slap_sasl_close( c );
717 if ( c->c_currentber != NULL ) {
718 ber_free( c->c_currentber, 1 );
719 c->c_currentber = NULL;
724 /* call destructors, then constructors; avoids unnecessary allocation */
725 if ( slapi_plugins_used ) {
726 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
730 c->c_conn_state = SLAP_C_INVALID;
731 c->c_struct_state = SLAP_C_UNUSED;
732 c->c_close_reason = "?"; /* should never be needed */
735 c->c_sb = ber_sockbuf_alloc( );
737 ber_len_t max = sockbuf_max_incoming;
738 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
741 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
743 /* c must be fully reset by this point; when we call slapd_remove
744 * it may get immediately reused by a new connection.
746 if ( sd != AC_SOCKET_INVALID ) {
747 slapd_remove( sd, sb, 1, 0, 0 );
749 if ( close_reason == NULL ) {
750 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
751 connid, (long) sd, 0, 0, 0 );
753 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
754 connid, (long) sd, close_reason, 0, 0 );
759 int connection_state_closing( Connection *c )
761 /* c_mutex must be locked by caller */
765 assert( c->c_struct_state == SLAP_C_USED );
767 state = c->c_conn_state;
769 assert( state != SLAP_C_INVALID );
771 return state == SLAP_C_CLOSING;
774 static void connection_abandon( Connection *c )
776 /* c_mutex must be locked by caller */
778 Operation *o, *next, op = {0};
784 op.o_connid = c->c_connid;
785 op.o_tag = LDAP_REQ_ABANDON;
787 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
788 next = LDAP_STAILQ_NEXT( o, o_next );
789 op.orn_msgid = o->o_msgid;
791 op.o_bd = frontendDB;
792 frontendDB->be_abandon( &op, &rs );
796 /* remove operations in pending transaction */
797 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
798 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
799 LDAP_STAILQ_NEXT(o, o_next) = NULL;
803 /* clear transaction */
804 c->c_txn_backend = NULL;
805 c->c_txn = CONN_TXN_INACTIVE;
808 /* remove pending operations */
809 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
810 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
811 LDAP_STAILQ_NEXT(o, o_next) = NULL;
816 void connection_closing( Connection *c, const char *why )
818 assert( connections != NULL );
820 assert( c->c_struct_state == SLAP_C_USED );
821 assert( c->c_conn_state != SLAP_C_INVALID );
823 /* c_mutex must be locked by caller */
825 if( c->c_conn_state != SLAP_C_CLOSING ) {
828 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
829 Debug( LDAP_DEBUG_TRACE,
830 "connection_closing: readying conn=%lu sd=%d for close\n",
831 c->c_connid, sd, 0 );
832 /* update state to closing */
833 c->c_conn_state = SLAP_C_CLOSING;
834 c->c_close_reason = why;
836 /* don't listen on this port anymore */
837 slapd_clr_read( sd, 0 );
839 /* abandon active operations */
840 connection_abandon( c );
842 /* wake write blocked operations */
843 if ( c->c_writewaiter ) {
844 ldap_pvt_thread_cond_signal( &c->c_write_cv );
845 /* ITS#4667 this may allow another thread to drop into
846 * connection_resched / connection_close before we
847 * finish, but that's OK.
849 slapd_clr_write( sd, 1 );
850 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
851 ldap_pvt_thread_mutex_lock( &c->c_write_mutex );
852 ldap_pvt_thread_mutex_lock( &c->c_mutex );
853 ldap_pvt_thread_mutex_unlock( &c->c_write_mutex );
855 slapd_clr_write( sd, 1 );
858 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
859 /* Client closed connection after doing Unbind. */
860 c->c_close_reason = NULL;
865 connection_close( Connection *c )
867 ber_socket_t sd = AC_SOCKET_INVALID;
869 assert( connections != NULL );
872 /* ITS#4667 we may have gotten here twice */
873 if ( c->c_conn_state == SLAP_C_INVALID )
876 assert( c->c_struct_state == SLAP_C_USED );
877 assert( c->c_conn_state == SLAP_C_CLOSING );
879 /* NOTE: c_mutex should be locked by caller */
881 /* NOTE: don't get the file descriptor if not needed */
883 if ( slap_debug & LDAP_DEBUG_TRACE ) {
884 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
886 #endif /* LDAP_DEBUG */
888 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
889 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
891 Debug( LDAP_DEBUG_TRACE,
892 "connection_close: deferring conn=%lu sd=%d\n",
893 c->c_connid, sd, 0 );
897 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
898 c->c_connid, sd, 0 );
900 connection_destroy( c );
903 unsigned long connections_nextid(void)
906 assert( connections != NULL );
908 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
912 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
917 Connection* connection_first( ber_socket_t *index )
919 assert( connections != NULL );
920 assert( index != NULL );
922 ldap_pvt_thread_mutex_lock( &connections_mutex );
923 for( *index = 0; *index < dtblsize; (*index)++) {
924 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
928 ldap_pvt_thread_mutex_unlock( &connections_mutex );
930 return connection_next(NULL, index);
933 Connection* connection_next( Connection *c, ber_socket_t *index )
935 assert( connections != NULL );
936 assert( index != NULL );
937 assert( *index <= dtblsize );
939 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
943 ldap_pvt_thread_mutex_lock( &connections_mutex );
944 for(; *index < dtblsize; (*index)++) {
946 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
947 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
955 if( connections[*index].c_struct_state == SLAP_C_USED ) {
956 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
957 c = &connections[(*index)++];
958 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
960 ldap_pvt_thread_mutex_unlock( &connections_mutex );
961 ldap_pvt_thread_mutex_lock( &c->c_mutex );
962 ldap_pvt_thread_mutex_lock( &connections_mutex );
963 if ( c->c_struct_state != SLAP_C_USED ) {
964 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
972 c_struct = connections[*index].c_struct_state;
973 if ( c_struct == SLAP_C_PENDING )
975 assert( c_struct == SLAP_C_UNUSED );
976 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
979 ldap_pvt_thread_mutex_unlock( &connections_mutex );
983 void connection_done( Connection *c )
985 assert( connections != NULL );
987 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
991 * connection_activity - handle the request operation op on connection
992 * conn. This routine figures out what kind of operation it is and
993 * calls the appropriate stub to handle it.
997 /* FIXME: returns 0 in case of failure */
998 #define INCR_OP_INITIATED(index) \
1000 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1001 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1002 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1004 #define INCR_OP_COMPLETED(index) \
1006 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1007 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1008 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1009 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1011 #else /* !SLAPD_MONITOR */
1012 #define INCR_OP_INITIATED(index) do { } while (0)
1013 #define INCR_OP_COMPLETED(index) \
1015 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1016 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1017 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1019 #endif /* !SLAPD_MONITOR */
1022 * NOTE: keep in sync with enum in slapd.h
1024 static BI_op_func *opfun[] = {
1039 connection_operation( void *ctx, void *arg_v )
1041 int rc = LDAP_OTHER;
1042 Operation *op = arg_v;
1043 SlapReply rs = {REP_RESULT};
1044 ber_tag_t tag = op->o_tag;
1045 slap_op_t opidx = SLAP_OP_LAST;
1046 Connection *conn = op->o_conn;
1047 void *memctx = NULL;
1048 void *memctx_null = NULL;
1051 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1052 /* FIXME: returns 0 in case of failure */
1053 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1054 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1056 op->o_threadctx = ctx;
1058 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1059 #endif /* LDAP_DEVEL */
1063 case LDAP_REQ_UNBIND:
1065 case LDAP_REQ_DELETE:
1066 case LDAP_REQ_MODDN:
1067 case LDAP_REQ_MODIFY:
1068 case LDAP_REQ_COMPARE:
1069 case LDAP_REQ_SEARCH:
1070 case LDAP_REQ_ABANDON:
1071 case LDAP_REQ_EXTENDED:
1074 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1075 "conn %lu unknown LDAP request 0x%lx\n",
1076 conn->c_connid, tag, 0 );
1077 op->o_tag = LBER_ERROR;
1078 rs.sr_err = LDAP_PROTOCOL_ERROR;
1079 rs.sr_text = "unknown LDAP request";
1080 send_ldap_disconnect( op, &rs );
1081 rc = SLAPD_DISCONNECT;
1082 goto operations_error;
1085 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1086 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1087 "error: SASL bind in progress (tag=%ld).\n",
1089 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1090 "SASL bind in progress" );
1091 rc = LDAP_OPERATIONS_ERROR;
1092 goto operations_error;
1096 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1097 ( tag == LDAP_REQ_ADD ) ||
1098 ( tag == LDAP_REQ_DELETE ) ||
1099 ( tag == LDAP_REQ_MODIFY ) ||
1100 ( tag == LDAP_REQ_MODRDN )))
1102 /* Disable SLAB allocator for all update operations
1103 issued inside of a transaction */
1104 op->o_tmpmemctx = NULL;
1105 op->o_tmpmfuncs = &ch_mfuncs;
1109 /* We can use Thread-Local storage for most mallocs. We can
1110 * also use TL for ber parsing, but not on Add or Modify.
1113 memsiz = ber_len( op->o_ber ) * 64;
1114 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1116 memsiz = SLAP_SLAB_SIZE;
1118 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1119 op->o_tmpmemctx = memctx;
1120 op->o_tmpmfuncs = &slap_sl_mfuncs;
1121 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1122 /* Note - the ber and its buffer are already allocated from
1123 * regular memory; this only affects subsequent mallocs that
1124 * ber_scanf may invoke.
1126 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1130 opidx = slap_req2op( tag );
1131 assert( opidx != SLAP_OP_LAST );
1132 INCR_OP_INITIATED( opidx );
1133 rc = (*(opfun[opidx]))( op, &rs );
1136 if ( rc == SLAPD_DISCONNECT ) {
1139 } else if ( opidx != SLAP_OP_LAST ) {
1140 /* increment completed operations count
1141 * only if operation was initiated
1142 * and rc != SLAPD_DISCONNECT */
1143 INCR_OP_COMPLETED( opidx );
1146 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1147 if ( rc == SLAPD_ABANDON ) {
1148 op->o_cancel = SLAP_CANCEL_ACK;
1150 op->o_cancel = LDAP_TOO_LATE;
1154 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1155 op->o_cancel != SLAP_CANCEL_DONE )
1157 ldap_pvt_thread_yield();
1160 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1162 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1164 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1165 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1167 conn->c_n_ops_executing--;
1168 conn->c_n_ops_completed++;
1172 case LDAP_REQ_UNBIND:
1173 /* c_mutex is locked */
1174 connection_closing( conn,
1175 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1179 connection_resched( conn );
1180 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1184 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1186 int connection_client_setup(
1188 ldap_pvt_thread_start_t *func,
1194 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1195 CONN_IS_CLIENT, 0, NULL );
1196 if ( rc < 0 ) return -1;
1198 c = connection_get( s );
1199 c->c_clientfunc = func;
1200 c->c_clientarg = arg;
1202 slapd_add_internal( s, 0 );
1203 slapd_set_read( s, 1 );
1204 connection_return( c );
1208 void connection_client_enable(
1211 slapd_set_read( s, 1 );
1214 void connection_client_stop(
1220 /* get (locked) connection */
1221 c = connection_get( s );
1223 assert( c->c_conn_state == SLAP_C_CLIENT );
1225 c->c_listener = NULL;
1226 c->c_conn_state = SLAP_C_INVALID;
1227 c->c_struct_state = SLAP_C_UNUSED;
1228 c->c_close_reason = "?"; /* should never be needed */
1230 c->c_sb = ber_sockbuf_alloc( );
1232 ber_len_t max = sockbuf_max_incoming;
1233 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1235 slapd_remove( s, sb, 0, 1, 0 );
1237 connection_return( c );
1240 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1242 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1244 static void* connection_read_thread( void* ctx, void* argv )
1247 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1248 ber_socket_t s = (long)argv;
1251 * read incoming LDAP requests. If there is more than one,
1252 * the first one is returned with new_op
1254 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1255 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1256 return (void*)(long)rc;
1259 /* execute a single queued request in the same thread */
1260 if( cri.op && !cri.nullop ) {
1261 rc = (long)connection_operation( ctx, cri.op );
1262 } else if ( cri.func ) {
1263 rc = (long)cri.func( ctx, cri.arg );
1266 return (void*)(long)rc;
1269 int connection_read_activate( ber_socket_t s )
1274 * suspend reading on this file descriptor until a connection processing
1275 * thread reads data on it. Otherwise the listener thread will repeatedly
1276 * submit the same event on it to the pool.
1278 rc = slapd_clr_read( s, 0 );
1282 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1283 connection_read_thread, (void *)(long)s );
1286 Debug( LDAP_DEBUG_ANY,
1287 "connection_read_activate(%d): submit failed (%d)\n",
1295 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1297 connection_read( ber_socket_t s, conn_readinfo *cri )
1299 int connection_read(ber_socket_t s)
1305 assert( connections != NULL );
1307 /* get (locked) connection */
1308 c = connection_get( s );
1311 Debug( LDAP_DEBUG_ANY,
1312 "connection_read(%ld): no connection!\n",
1320 if( c->c_conn_state == SLAP_C_CLOSING ) {
1321 Debug( LDAP_DEBUG_TRACE,
1322 "connection_read(%d): closing, ignoring input for id=%lu\n",
1323 s, c->c_connid, 0 );
1324 connection_return( c );
1328 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1329 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1330 cri->func = c->c_clientfunc;
1331 cri->arg = c->c_clientarg;
1332 /* read should already be cleared */
1334 slapd_clr_read( s, 0 );
1335 ldap_pvt_thread_pool_submit( &connection_pool,
1336 c->c_clientfunc, c->c_clientarg );
1338 connection_return( c );
1342 Debug( LDAP_DEBUG_TRACE,
1343 "connection_read(%d): checking for input on id=%lu\n",
1344 s, c->c_connid, 0 );
1347 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1348 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1350 Debug( LDAP_DEBUG_TRACE,
1351 "connection_read(%d): TLS accept failure "
1352 "error=%d id=%lu, closing\n",
1353 s, rc, c->c_connid );
1355 c->c_needs_tls_accept = 0;
1356 /* c_mutex is locked */
1357 connection_closing( c, "TLS negotiation failure" );
1358 connection_close( c );
1359 connection_return( c );
1362 } else if ( rc == 0 ) {
1364 struct berval authid = BER_BVNULL;
1366 c->c_needs_tls_accept = 0;
1368 /* we need to let SASL know */
1369 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1371 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1372 if( c->c_tls_ssf > c->c_ssf ) {
1373 c->c_ssf = c->c_tls_ssf;
1376 rc = dnX509peerNormalize( ssl, &authid );
1377 if ( rc != LDAP_SUCCESS ) {
1378 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1379 "unable to get TLS client DN, error=%d id=%lu\n",
1380 s, rc, c->c_connid );
1382 Statslog( LDAP_DEBUG_STATS,
1383 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1384 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1385 slap_sasl_external( c, c->c_tls_ssf, &authid );
1386 if ( authid.bv_val ) free( authid.bv_val );
1389 /* if success and data is ready, fall thru to data input loop */
1390 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1392 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1393 slapd_set_read( s, 1 );
1396 connection_return( c );
1402 #ifdef HAVE_CYRUS_SASL
1403 if ( c->c_sasl_layers ) {
1404 /* If previous layer is not removed yet, give up for now */
1405 if ( !c->c_sasl_sockctx ) {
1406 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1407 slapd_set_read( s, 1 );
1410 connection_return( c );
1414 c->c_sasl_layers = 0;
1416 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1417 if( rc != LDAP_SUCCESS ) {
1418 Debug( LDAP_DEBUG_TRACE,
1419 "connection_read(%d): SASL install error "
1420 "error=%d id=%lu, closing\n",
1421 s, rc, c->c_connid );
1423 /* c_mutex is locked */
1424 connection_closing( c, "SASL layer install failure" );
1425 connection_close( c );
1426 connection_return( c );
1432 #define CONNECTION_INPUT_LOOP 1
1433 /* #define DATA_READY_LOOP 1 */
1436 /* How do we do this without getting into a busy loop ? */
1437 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1438 rc = connection_input( c, cri );
1440 rc = connection_input( c );
1443 #ifdef DATA_READY_LOOP
1444 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1445 #elif CONNECTION_INPUT_LOOP
1452 Debug( LDAP_DEBUG_CONNS,
1453 "connection_read(%d): input error=%d id=%lu, closing.\n",
1454 s, rc, c->c_connid );
1456 /* c_mutex is locked */
1457 connection_closing( c, conn_lost_str );
1458 connection_close( c );
1459 connection_return( c );
1463 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1464 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1465 slapd_set_write( s, 0 );
1468 slapd_set_read( s, 1 );
1470 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1471 slapd_set_read( s, 1 );
1474 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1475 slapd_set_write( s, 1 );
1479 connection_return( c );
1485 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1486 connection_input( Connection *conn , conn_readinfo *cri )
1488 connection_input( Connection *conn )
1497 #ifdef LDAP_CONNECTIONLESS
1503 if ( conn->c_currentber == NULL &&
1504 ( conn->c_currentber = ber_alloc()) == NULL )
1506 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1512 #ifdef LDAP_CONNECTIONLESS
1513 if ( conn->c_is_udp ) {
1514 char peername[sizeof("IP=255.255.255.255:65336")];
1516 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1517 if (len != sizeof(struct sockaddr)) return 1;
1519 sprintf( peername, "IP=%s:%d",
1520 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1521 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1522 Statslog( LDAP_DEBUG_STATS,
1523 "conn=%lu UDP request from %s (%s) accepted.\n",
1524 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1528 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1529 if ( tag != LDAP_TAG_MESSAGE ) {
1530 int err = sock_errno();
1533 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1535 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1536 /* log, close and send error */
1537 Debug( LDAP_DEBUG_TRACE,
1538 "ber_get_next on fd %d failed errno=%d (%s)\n",
1539 sd, err, sock_errstr(err) );
1540 ber_free( conn->c_currentber, 1 );
1541 conn->c_currentber = NULL;
1548 ber = conn->c_currentber;
1549 conn->c_currentber = NULL;
1551 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1552 /* log, close and send error */
1553 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1558 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1559 /* log, close and send error */
1560 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1566 #ifdef LDAP_CONNECTIONLESS
1567 if( conn->c_is_udp ) {
1568 if( tag == LBER_OCTETSTRING ) {
1569 ber_get_stringa( ber, &cdn );
1570 tag = ber_peek_tag(ber, &len);
1572 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1573 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1580 if(tag == LDAP_REQ_BIND) {
1581 /* immediately abandon all existing operations upon BIND */
1582 connection_abandon( conn );
1585 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1588 /* clear state if the connection is being reused from inactive */
1589 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1590 memset( &conn->c_pagedresults_state, 0,
1591 sizeof( conn->c_pagedresults_state ) );
1594 op->o_res_ber = NULL;
1596 #ifdef LDAP_CONNECTIONLESS
1597 if (conn->c_is_udp) {
1599 ber_str2bv( cdn, 0, 1, &op->o_dn );
1600 op->o_protocol = LDAP_VERSION2;
1602 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1603 if (op->o_res_ber == NULL) return 1;
1605 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1606 sizeof(struct sockaddr), 0 );
1608 if (rc != sizeof(struct sockaddr)) {
1609 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1613 if (op->o_protocol == LDAP_VERSION2) {
1614 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1616 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1621 #endif /* LDAP_CONNECTIONLESS */
1625 /* Don't process requests when the conn is in the middle of a
1626 * Bind, or if it's closing. Also, don't let any single conn
1627 * use up all the available threads, and don't execute if we're
1628 * currently blocked on output. And don't execute if there are
1629 * already pending ops, let them go first. Abandon operations
1630 * get exceptions to some, but not all, cases.
1634 /* Abandon and Unbind are exempt from these checks */
1635 if (conn->c_conn_state == SLAP_C_CLOSING) {
1638 } else if (conn->c_writewaiter) {
1639 defer = "awaiting write";
1641 } else if (conn->c_n_ops_pending) {
1642 defer = "pending operations";
1646 case LDAP_REQ_ABANDON:
1647 /* Unbind is exempt from these checks */
1648 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1649 defer = "too many executing";
1651 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1656 case LDAP_REQ_UNBIND:
1661 int max = conn->c_dn.bv_len
1662 ? slap_conn_max_pending_auth
1663 : slap_conn_max_pending;
1665 Debug( LDAP_DEBUG_ANY,
1666 "connection_input: conn=%lu deferring operation: %s\n",
1667 conn->c_connid, defer, 0 );
1668 conn->c_n_ops_pending++;
1669 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1670 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1673 conn->c_n_ops_executing++;
1675 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1677 * The first op will be processed in the same thread context,
1678 * as long as there is only one op total.
1679 * Subsequent ops will be submitted to the pool by
1680 * calling connection_op_activate()
1682 if ( cri->op == NULL ) {
1683 /* the first incoming request */
1684 connection_op_queue( op );
1687 if ( !cri->nullop ) {
1689 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1690 connection_operation, (void *) cri->op );
1692 connection_op_activate( op );
1695 connection_op_activate( op );
1700 if ( conn->c_struct_state != SLAP_C_USED ) {
1701 /* connection must have got closed underneath us */
1706 assert( conn->c_struct_state == SLAP_C_USED );
1711 connection_resched( Connection *conn )
1715 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1717 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1719 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1720 "attempting closing conn=%lu sd=%d\n",
1721 conn->c_connid, sd, 0 );
1722 connection_close( conn );
1726 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1727 /* other states need different handling */
1731 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1732 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1734 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1735 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1737 /* pending operations should not be marked for abandonment */
1738 assert(!op->o_abandon);
1740 conn->c_n_ops_pending--;
1741 conn->c_n_ops_executing++;
1743 connection_op_activate( op );
1745 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1751 connection_init_log_prefix( Operation *op )
1753 if ( op->o_connid == (unsigned long)(-1) ) {
1754 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1755 "conn=-1 op=%lu", op->o_opid );
1758 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1759 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1763 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1765 op->o_conn->c_sasl_bindop = NULL;
1767 ch_free( op->o_callback );
1768 op->o_callback = NULL;
1770 return SLAP_CB_CONTINUE;
1773 static int connection_bind_cb( Operation *op, SlapReply *rs )
1775 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1776 if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1777 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1778 op->o_conn->c_sasl_bind_in_progress =
1779 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1781 /* Moved here from bind.c due to ITS#4158 */
1782 op->o_conn->c_sasl_bindop = NULL;
1783 if ( op->orb_method == LDAP_AUTH_SASL ) {
1784 if( rs->sr_err == LDAP_SUCCESS ) {
1785 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1786 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1787 /* edn is always normalized already */
1788 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1790 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1791 BER_BVZERO( &op->orb_edn );
1792 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1793 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1795 op->o_conn->c_sasl_ssf = op->orb_ssf;
1796 if( op->orb_ssf > op->o_conn->c_ssf ) {
1797 op->o_conn->c_ssf = op->orb_ssf;
1800 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1801 ber_len_t max = sockbuf_max_incoming_auth;
1802 ber_sockbuf_ctrl( op->o_conn->c_sb,
1803 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1806 /* log authorization identity */
1807 Statslog( LDAP_DEBUG_STATS,
1808 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
1810 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1811 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
1813 Debug( LDAP_DEBUG_TRACE,
1814 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
1815 op->o_conn->c_authmech.bv_val,
1816 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1819 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1820 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1821 free( op->o_conn->c_sasl_bind_mech.bv_val );
1822 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1826 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1828 ch_free( op->o_callback );
1829 op->o_callback = NULL;
1831 return SLAP_CB_CONTINUE;
1834 static void connection_op_queue( Operation *op )
1836 ber_tag_t tag = op->o_tag;
1838 if (tag == LDAP_REQ_BIND) {
1839 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1840 sc->sc_response = connection_bind_cb;
1841 sc->sc_cleanup = connection_bind_cleanup_cb;
1842 sc->sc_next = op->o_callback;
1843 op->o_callback = sc;
1844 op->o_conn->c_conn_state = SLAP_C_BINDING;
1847 if (!op->o_dn.bv_len) {
1848 op->o_authz = op->o_conn->c_authz;
1849 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1850 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1851 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1853 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1854 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1858 op->o_authtype = op->o_conn->c_authtype;
1859 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1861 if (!op->o_protocol) {
1862 op->o_protocol = op->o_conn->c_protocol
1863 ? op->o_conn->c_protocol : LDAP_VERSION3;
1866 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1867 op->o_protocol > LDAP_VERSION2)
1869 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1872 op->o_connid = op->o_conn->c_connid;
1873 connection_init_log_prefix( op );
1875 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1878 static int connection_op_activate( Operation *op )
1882 connection_op_queue( op );
1884 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1885 connection_operation, (void *) op );
1888 Debug( LDAP_DEBUG_ANY,
1889 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1890 rc, op->o_connid, 0 );
1891 /* should move op to pending list */
1897 int connection_write(ber_socket_t s)
1902 assert( connections != NULL );
1904 slapd_clr_write( s, 0 );
1906 c = connection_get( s );
1908 Debug( LDAP_DEBUG_ANY,
1909 "connection_write(%ld): no connection!\n",
1916 Debug( LDAP_DEBUG_TRACE,
1917 "connection_write(%d): waking output for id=%lu\n",
1918 s, c->c_connid, 0 );
1919 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1921 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1922 slapd_set_read( s, 1 );
1924 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1925 slapd_set_write( s, 1 );
1928 /* If there are ops pending because of a writewaiter,
1931 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1932 if ( !c->c_writewaiter ) break;
1933 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1935 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1936 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1938 /* pending operations should not be marked for abandonment */
1939 assert(!op->o_abandon);
1941 c->c_n_ops_pending--;
1942 c->c_n_ops_executing++;
1944 connection_op_activate( op );
1949 connection_return( c );
1954 connection_fake_init(
1959 conn->c_connid = -1;
1960 conn->c_send_ldap_result = slap_send_ldap_result;
1961 conn->c_send_search_entry = slap_send_search_entry;
1962 conn->c_send_search_reference = slap_send_search_reference;
1963 conn->c_listener = (Listener *)&dummy_list;
1964 conn->c_peer_domain = slap_empty_bv;
1965 conn->c_peer_name = slap_empty_bv;
1967 memset(op, 0, OPERATION_BUFFER_SIZE);
1968 op->o_hdr = (Opheader *)(op+1);
1969 op->o_controls = (void **)(op->o_hdr+1);
1970 /* set memory context */
1971 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
1972 op->o_tmpmfuncs = &slap_sl_mfuncs;
1973 op->o_threadctx = ctx;
1975 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1976 #endif /* LDAP_DEVEL */
1979 op->o_connid = op->o_conn->c_connid;
1980 connection_init_log_prefix( op );
1983 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
1984 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
1985 #endif /* LDAP_SLAPI */
1987 slap_op_time( &op->o_time, &op->o_tincr );
1991 connection_assign_nextid( Connection *conn )
1993 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1994 conn->c_connid = conn_nextid++;
1995 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );