3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/string.h>
16 #include <ac/unistd.h>
22 /* protected by connections_mutex */
23 static ldap_pvt_thread_mutex_t connections_mutex;
24 static Connection *connections = NULL;
25 static unsigned long conn_nextid = 0;
27 /* structure state (protected by connections_mutex) */
28 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
29 #define SLAP_C_UNUSED 0x01
30 #define SLAP_C_USED 0x02
32 /* connection state (protected by c_mutex ) */
33 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
34 #define SLAP_C_INACTIVE 0x01 /* zero threads */
35 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
36 #define SLAP_C_BINDING 0x03 /* binding */
37 #define SLAP_C_CLOSING 0x04 /* closing */
40 connection_state2str( int state )
43 case SLAP_C_INVALID: return "!";
44 case SLAP_C_INACTIVE: return "|";
45 case SLAP_C_ACTIVE: return "";
46 case SLAP_C_BINDING: return "B";
47 case SLAP_C_CLOSING: return "C";
53 static Connection* connection_get( ber_socket_t s );
55 static int connection_input( Connection *c );
56 static void connection_close( Connection *c );
58 static int connection_op_activate( Operation *op );
59 static int connection_resched( Connection *conn );
60 static void connection_abandon( Connection *conn );
61 static void connection_destroy( Connection *c );
63 static ldap_pvt_thread_start_t connection_operation;
66 * Initialize connection management infrastructure.
68 int connections_init(void)
70 assert( connections == NULL );
72 if( connections != NULL) {
74 LDAP_LOG( CONNECTION, INFO,
75 "connections_init: already initialized.\n", 0, 0, 0 );
77 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
83 /* should check return of every call */
84 ldap_pvt_thread_mutex_init( &connections_mutex );
86 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
88 if( connections == NULL ) {
90 LDAP_LOG( CONNECTION, ERR,
91 "connections_init: allocation (%d * %ld) of connection "
92 "array failed\n", dtblsize, (long) sizeof(Connection), 0 );
94 Debug( LDAP_DEBUG_ANY,
95 "connections_init: allocation (%d*%ld) of connection array failed\n",
96 dtblsize, (long) sizeof(Connection), 0 );
101 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
102 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
105 * per entry initialization of the Connection array initialization
106 * will be done by connection_init()
113 * Destroy connection management infrastructure.
115 int connections_destroy(void)
119 /* should check return of every call */
121 if( connections == NULL) {
123 LDAP_LOG( CONNECTION, INFO,
124 "connections_destroy: nothing to destroy.\n", 0, 0, 0 );
126 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
132 for ( i = 0; i < dtblsize; i++ ) {
133 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
134 ber_sockbuf_free( connections[i].c_sb );
135 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
136 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
137 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
144 ldap_pvt_thread_mutex_destroy( &connections_mutex );
149 * shutdown all connections
151 int connections_shutdown(void)
155 ldap_pvt_thread_mutex_lock( &connections_mutex );
157 for ( i = 0; i < dtblsize; i++ ) {
158 if( connections[i].c_struct_state != SLAP_C_USED ) {
162 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
164 /* connections_mutex and c_mutex are locked */
165 connection_closing( &connections[i] );
166 connection_close( &connections[i] );
168 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
171 ldap_pvt_thread_mutex_unlock( &connections_mutex );
177 * Timeout idle connections.
179 int connections_timeout_idle(time_t now)
185 for( c = connection_first( &connindex );
187 c = connection_next( c, &connindex ) )
189 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
191 connection_closing( c );
192 connection_close( c );
196 connection_done( c );
201 static Connection* connection_get( ber_socket_t s )
203 /* connections_mutex should be locked by caller */
208 LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
210 Debug( LDAP_DEBUG_ARGS,
211 "connection_get(%ld)\n",
215 assert( connections != NULL );
217 if(s == AC_SOCKET_INVALID) {
224 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
231 for(i=0; i<dtblsize; i++) {
232 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
233 assert( connections[i].c_conn_state == SLAP_C_INVALID );
234 assert( connections[i].c_sb == 0 );
238 ber_sockbuf_ctrl( connections[i].c_sb,
239 LBER_SB_OPT_GET_FD, &sd );
241 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
242 assert( connections[i].c_conn_state == SLAP_C_INVALID );
243 assert( sd == AC_SOCKET_INVALID );
247 /* state can actually change from used -> unused by resched,
248 * so don't assert details here.
262 ldap_pvt_thread_mutex_lock( &c->c_mutex );
264 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
265 if( c->c_struct_state != SLAP_C_USED ) {
266 /* connection must have been closed due to resched */
268 assert( c->c_conn_state == SLAP_C_INVALID );
269 assert( sd == AC_SOCKET_INVALID );
272 LDAP_LOG( CONNECTION, ARGS,
273 "connection_get: connection %d not used\n", s, 0, 0 );
275 Debug( LDAP_DEBUG_TRACE,
276 "connection_get(%d): connection not used\n",
280 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
285 LDAP_LOG( CONNECTION, RESULTS,
286 "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
288 Debug( LDAP_DEBUG_TRACE,
289 "connection_get(%d): got connid=%lu\n",
295 assert( c->c_struct_state == SLAP_C_USED );
296 assert( c->c_conn_state != SLAP_C_INVALID );
297 assert( sd != AC_SOCKET_INVALID );
300 c->c_activitytime = slap_get_time();
302 if( global_idletimeout > 0 ) {
303 c->c_activitytime = slap_get_time();
311 static void connection_return( Connection *c )
313 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
316 long connection_init(
320 const char* peername,
328 assert( connections != NULL );
330 assert( listener != NULL );
331 assert( dnsname != NULL );
332 assert( peername != NULL );
335 assert( tls_udp_option != 1 );
338 if( s == AC_SOCKET_INVALID ) {
340 LDAP_LOG( CONNECTION, INFO,
341 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
343 Debug( LDAP_DEBUG_ANY,
344 "connection_init(%ld): invalid.\n",
352 assert( s < dtblsize );
355 ldap_pvt_thread_mutex_lock( &connections_mutex );
366 for( i=0; i < dtblsize; i++) {
369 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
370 assert( connections[i].c_sb == 0 );
375 sd = AC_SOCKET_INVALID;
376 if (connections[i].c_sb != NULL)
377 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
379 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
380 assert( sd == AC_SOCKET_INVALID );
385 assert( connections[i].c_struct_state == SLAP_C_USED );
386 assert( connections[i].c_conn_state != SLAP_C_INVALID );
387 assert( sd != AC_SOCKET_INVALID );
392 LDAP_LOG( CONNECTION, INFO,
393 "connection_init: skt %d connection table full "
394 "(%d/%d)\n", s, i, dtblsize );
396 Debug( LDAP_DEBUG_ANY,
397 "connection_init(%d): connection table full "
398 "(%d/%d)\n", s, i, dtblsize);
400 ldap_pvt_thread_mutex_unlock( &connections_mutex );
408 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
409 c->c_send_ldap_result = slap_send_ldap_result;
410 c->c_send_search_entry = slap_send_search_entry;
411 c->c_send_search_reference = slap_send_search_reference;
412 c->c_send_ldap_extended = slap_send_ldap_extended;
413 #ifdef LDAP_RES_INTERMEDIATE_RESP
414 c->c_send_ldap_intermediate_resp = slap_send_ldap_intermediate_resp;
417 c->c_authmech.bv_val = NULL;
418 c->c_authmech.bv_len = 0;
419 c->c_dn.bv_val = NULL;
421 c->c_ndn.bv_val = NULL;
425 c->c_listener = NULL;
426 c->c_peer_domain.bv_val = NULL;
427 c->c_peer_domain.bv_len = 0;
428 c->c_peer_name.bv_val = NULL;
429 c->c_peer_name.bv_len = 0;
431 LDAP_STAILQ_INIT(&c->c_ops);
432 LDAP_STAILQ_INIT(&c->c_pending_ops);
434 c->c_sasl_bind_mech.bv_val = NULL;
435 c->c_sasl_bind_mech.bv_len = 0;
437 c->c_sasl_authctx = NULL;
438 c->c_sasl_sockctx = NULL;
439 c->c_sasl_extra = NULL;
440 c->c_sasl_bindop = NULL;
442 c->c_sb = ber_sockbuf_alloc( );
445 ber_len_t max = sockbuf_max_incoming;
446 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
449 c->c_currentber = NULL;
451 /* should check status of thread calls */
452 ldap_pvt_thread_mutex_init( &c->c_mutex );
453 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
454 ldap_pvt_thread_cond_init( &c->c_write_cv );
456 c->c_struct_state = SLAP_C_UNUSED;
459 ldap_pvt_thread_mutex_lock( &c->c_mutex );
461 assert( c->c_struct_state == SLAP_C_UNUSED );
462 assert( c->c_authmech.bv_val == NULL );
463 assert( c->c_dn.bv_val == NULL );
464 assert( c->c_ndn.bv_val == NULL );
465 assert( c->c_groups == NULL );
466 assert( c->c_listener == NULL );
467 assert( c->c_peer_domain.bv_val == NULL );
468 assert( c->c_peer_name.bv_val == NULL );
469 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
470 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
471 assert( c->c_sasl_bind_mech.bv_val == NULL );
472 assert( c->c_sasl_done == 0 );
473 assert( c->c_sasl_authctx == NULL );
474 assert( c->c_sasl_sockctx == NULL );
475 assert( c->c_sasl_extra == NULL );
476 assert( c->c_sasl_bindop == NULL );
477 assert( c->c_currentber == NULL );
479 c->c_listener = listener;
480 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
481 ber_str2bv( peername, 0, 1, &c->c_peer_name );
483 c->c_n_ops_received = 0;
484 c->c_n_ops_executing = 0;
485 c->c_n_ops_pending = 0;
486 c->c_n_ops_completed = 0;
492 /* set to zero until bind, implies LDAP_VERSION3 */
496 c->c_activitytime = c->c_starttime = slap_get_time();
498 if( global_idletimeout > 0 ) {
499 c->c_activitytime = c->c_starttime = slap_get_time();
503 #ifdef LDAP_CONNECTIONLESS
505 if( tls_udp_option == 2 ) {
508 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
509 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
511 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
512 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
513 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
514 LBER_SBIOD_LEVEL_PROVIDER, NULL );
519 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
520 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
522 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
523 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
527 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
528 INT_MAX, (void*)"ldap_" );
531 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
532 c /* non-NULL */ ) < 0 )
535 LDAP_LOG( CONNECTION, INFO,
536 "connection_init: conn %lu set nonblocking failed\n",
539 Debug( LDAP_DEBUG_ANY,
540 "connection_init(%d, %s): set nonblocking failed\n",
541 s, c->c_peer_name.bv_val, 0 );
545 id = c->c_connid = conn_nextid++;
547 c->c_conn_state = SLAP_C_INACTIVE;
548 c->c_struct_state = SLAP_C_USED;
550 c->c_ssf = c->c_transport_ssf = ssf;
554 if ( tls_udp_option == 1 ) {
556 c->c_needs_tls_accept = 1;
559 c->c_needs_tls_accept = 0;
563 slap_sasl_open( c, 0 );
564 slap_sasl_external( c, ssf, authid );
566 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
567 ldap_pvt_thread_mutex_unlock( &connections_mutex );
569 backend_connection_init(c);
574 void connection2anonymous( Connection *c )
576 assert( connections != NULL );
580 ber_len_t max = sockbuf_max_incoming;
581 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
584 if(c->c_authmech.bv_val != NULL ) {
585 free(c->c_authmech.bv_val);
586 c->c_authmech.bv_val = NULL;
588 c->c_authmech.bv_len = 0;
590 if(c->c_dn.bv_val != NULL) {
591 free(c->c_dn.bv_val);
592 c->c_dn.bv_val = NULL;
595 if(c->c_ndn.bv_val != NULL) {
596 free(c->c_ndn.bv_val);
597 c->c_ndn.bv_val = NULL;
601 c->c_authz_backend = NULL;
604 GroupAssertion *g, *n;
605 for (g = c->c_groups; g; g=n) {
614 connection_destroy( Connection *c )
616 /* note: connections_mutex should be locked by caller */
618 unsigned long connid;
620 assert( connections != NULL );
622 assert( c->c_struct_state != SLAP_C_UNUSED );
623 assert( c->c_conn_state != SLAP_C_INVALID );
624 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
626 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
627 connid = c->c_connid;
629 backend_connection_destroy(c);
634 c->c_activitytime = c->c_starttime = 0;
636 connection2anonymous( c );
637 c->c_listener = NULL;
639 if(c->c_peer_domain.bv_val != NULL) {
640 free(c->c_peer_domain.bv_val);
641 c->c_peer_domain.bv_val = NULL;
643 c->c_peer_domain.bv_len = 0;
644 if(c->c_peer_name.bv_val != NULL) {
645 free(c->c_peer_name.bv_val);
646 c->c_peer_name.bv_val = NULL;
648 c->c_peer_name.bv_len = 0;
650 c->c_sasl_bind_in_progress = 0;
651 if(c->c_sasl_bind_mech.bv_val != NULL) {
652 free(c->c_sasl_bind_mech.bv_val);
653 c->c_sasl_bind_mech.bv_val = NULL;
655 c->c_sasl_bind_mech.bv_len = 0;
657 slap_sasl_close( c );
659 if ( c->c_currentber != NULL ) {
660 ber_free( c->c_currentber, 1 );
661 c->c_currentber = NULL;
664 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
665 if ( sd != AC_SOCKET_INVALID ) {
666 slapd_remove( sd, 0 );
668 Statslog( LDAP_DEBUG_STATS,
669 "conn=%lu fd=%ld closed\n",
670 connid, (long) sd, 0, 0, 0 );
673 ber_sockbuf_free( c->c_sb );
675 c->c_sb = ber_sockbuf_alloc( );
678 ber_len_t max = sockbuf_max_incoming;
679 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
682 c->c_conn_state = SLAP_C_INVALID;
683 c->c_struct_state = SLAP_C_UNUSED;
686 int connection_state_closing( Connection *c )
688 /* c_mutex must be locked by caller */
692 assert( c->c_struct_state == SLAP_C_USED );
694 state = c->c_conn_state;
696 assert( state != SLAP_C_INVALID );
698 return state == SLAP_C_CLOSING;
701 static void connection_abandon( Connection *c )
703 /* c_mutex must be locked by caller */
707 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
711 /* remove pending operations */
712 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
713 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
714 LDAP_STAILQ_NEXT(o, o_next) = NULL;
719 void connection_closing( Connection *c )
721 assert( connections != NULL );
723 assert( c->c_struct_state == SLAP_C_USED );
724 assert( c->c_conn_state != SLAP_C_INVALID );
726 /* c_mutex must be locked by caller */
728 if( c->c_conn_state != SLAP_C_CLOSING ) {
731 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
733 LDAP_LOG( CONNECTION, DETAIL1,
734 "connection_closing: conn %lu readying socket %d for close.\n",
735 c->c_connid, sd, 0 );
737 Debug( LDAP_DEBUG_TRACE,
738 "connection_closing: readying conn=%lu sd=%d for close\n",
739 c->c_connid, sd, 0 );
741 /* update state to closing */
742 c->c_conn_state = SLAP_C_CLOSING;
744 /* don't listen on this port anymore */
745 slapd_clr_read( sd, 1 );
747 /* abandon active operations */
748 connection_abandon( c );
750 /* wake write blocked operations */
751 slapd_clr_write( sd, 1 );
752 ldap_pvt_thread_cond_signal( &c->c_write_cv );
756 static void connection_close( Connection *c )
760 assert( connections != NULL );
762 assert( c->c_struct_state == SLAP_C_USED );
763 assert( c->c_conn_state == SLAP_C_CLOSING );
765 /* note: connections_mutex and c_mutex should be locked by caller */
767 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
768 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
770 LDAP_LOG( CONNECTION, DETAIL1,
771 "connection_close: conn %lu deferring sd %d\n",
772 c->c_connid, sd, 0 );
774 Debug( LDAP_DEBUG_TRACE,
775 "connection_close: deferring conn=%lu sd=%d\n",
776 c->c_connid, sd, 0 );
782 LDAP_LOG( CONNECTION, RESULTS,
783 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
785 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
786 c->c_connid, sd, 0 );
788 connection_destroy( c );
791 unsigned long connections_nextid(void)
794 assert( connections != NULL );
796 ldap_pvt_thread_mutex_lock( &connections_mutex );
800 ldap_pvt_thread_mutex_unlock( &connections_mutex );
805 Connection* connection_first( ber_socket_t *index )
807 assert( connections != NULL );
808 assert( index != NULL );
810 ldap_pvt_thread_mutex_lock( &connections_mutex );
814 return connection_next(NULL, index);
817 Connection* connection_next( Connection *c, ber_socket_t *index )
819 assert( connections != NULL );
820 assert( index != NULL );
821 assert( *index <= dtblsize );
824 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
829 for(; *index < dtblsize; (*index)++) {
830 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
831 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
839 if( connections[*index].c_struct_state == SLAP_C_USED ) {
840 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
841 c = &connections[(*index)++];
845 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
846 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
850 ldap_pvt_thread_mutex_lock( &c->c_mutex );
856 void connection_done( Connection *c )
858 assert( connections != NULL );
861 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
864 ldap_pvt_thread_mutex_unlock( &connections_mutex );
868 * connection_activity - handle the request operation op on connection
869 * conn. This routine figures out what kind of operation it is and
870 * calls the appropriate stub to handle it.
874 #define INCR_OP(var,index) \
876 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
878 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
880 #else /* !SLAPD_MONITOR */
881 #define INCR_OP(var,index)
882 #endif /* !SLAPD_MONITOR */
885 connection_operation( void *ctx, void *arg_v )
887 int rc = SLAPD_DISCONNECT;
888 Operation *op = arg_v;
889 SlapReply rs = {REP_RESULT};
890 ber_tag_t tag = op->o_tag;
892 ber_tag_t oldtag = tag;
893 #endif /* SLAPD_MONITOR */
894 Connection *conn = op->o_conn;
898 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
900 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
902 op->o_threadctx = ctx;
904 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
906 LDAP_LOG( CONNECTION, ERR,
907 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
908 conn->c_connid, (long)tag, 0 );
910 Debug( LDAP_DEBUG_ANY, "connection_operation: "
911 "error: SASL bind in progress (tag=%ld).\n",
914 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
915 "SASL bind in progress" );
916 goto operations_error;
919 /* We can use Thread-Local storage for most mallocs. We can
920 * also use TL for ber parsing, but not on Add or Modify.
922 #define SLAB_SIZE 1048576
924 memsiz = ber_len( op->o_ber ) * 64;
925 if ( SLAB_SIZE > memsiz ) memsiz = SLAB_SIZE;
929 memctx = sl_mem_create( memsiz, ctx );
930 op->o_tmpmemctx = memctx;
931 op->o_tmpmfuncs = &sl_mfuncs;
932 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
933 /* Note - the ber and its buffer are already allocated from
934 * regular memory; this only affects subsequent mallocs that
935 * ber_scanf may invoke.
937 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
942 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
943 rc = do_bind( op, &rs );
946 case LDAP_REQ_UNBIND:
947 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
948 rc = do_unbind( op, &rs );
952 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
953 rc = do_add( op, &rs );
956 case LDAP_REQ_DELETE:
957 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
958 rc = do_delete( op, &rs );
961 case LDAP_REQ_MODRDN:
962 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
963 rc = do_modrdn( op, &rs );
966 case LDAP_REQ_MODIFY:
967 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
968 rc = do_modify( op, &rs );
971 case LDAP_REQ_COMPARE:
972 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
973 rc = do_compare( op, &rs );
976 case LDAP_REQ_SEARCH:
977 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
978 rc = do_search( op, &rs );
981 case LDAP_REQ_ABANDON:
982 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
983 rc = do_abandon( op, &rs );
986 case LDAP_REQ_EXTENDED:
987 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
988 rc = do_extended( op, &rs );
993 LDAP_LOG( CONNECTION, INFO,
994 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
995 conn->c_connid, tag, 0 );
997 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
1000 op->o_tag = LBER_ERROR;
1001 rs.sr_err = LDAP_PROTOCOL_ERROR;
1002 rs.sr_text = "unknown LDAP request";
1003 send_ldap_disconnect( op, &rs );
1008 #ifdef SLAPD_MONITOR
1010 #endif /* SLAPD_MONITOR */
1011 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1014 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1015 num_ops_completed++;
1016 #ifdef SLAPD_MONITOR
1019 num_ops_completed_[SLAP_OP_BIND]++;
1021 case LDAP_REQ_UNBIND:
1022 num_ops_completed_[SLAP_OP_UNBIND]++;
1025 num_ops_completed_[SLAP_OP_ADD]++;
1027 case LDAP_REQ_DELETE:
1028 num_ops_completed_[SLAP_OP_DELETE]++;
1030 case LDAP_REQ_MODRDN:
1031 num_ops_completed_[SLAP_OP_MODRDN]++;
1033 case LDAP_REQ_MODIFY:
1034 num_ops_completed_[SLAP_OP_MODIFY]++;
1036 case LDAP_REQ_COMPARE:
1037 num_ops_completed_[SLAP_OP_COMPARE]++;
1039 case LDAP_REQ_SEARCH:
1040 num_ops_completed_[SLAP_OP_SEARCH]++;
1042 case LDAP_REQ_ABANDON:
1043 num_ops_completed_[SLAP_OP_ABANDON]++;
1045 case LDAP_REQ_EXTENDED:
1046 num_ops_completed_[SLAP_OP_EXTENDED]++;
1049 #endif /* SLAPD_MONITOR */
1050 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1052 #ifdef LDAP_EXOP_X_CANCEL
1053 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1054 op->o_cancel = LDAP_TOO_LATE;
1057 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1058 op->o_cancel != SLAP_CANCEL_DONE )
1060 ldap_pvt_thread_yield();
1064 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1066 conn->c_n_ops_executing--;
1067 conn->c_n_ops_completed++;
1069 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1070 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1072 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1073 if ( op->o_cancel == SLAP_CANCEL_ACK )
1076 #ifdef LDAP_CLIENT_UPDATE
1077 if ( ( op->o_clientupdate_type & SLAP_LCUP_PERSIST ) ) {
1078 sl_mem_detach( ctx, memctx );
1083 if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) {
1084 sl_mem_detach( ctx, memctx );
1092 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1099 case LDAP_REQ_UNBIND:
1100 /* c_mutex is locked */
1101 connection_closing( conn );
1105 conn->c_sasl_bind_in_progress =
1106 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1108 if( conn->c_conn_state == SLAP_C_BINDING) {
1109 conn->c_conn_state = SLAP_C_ACTIVE;
1113 connection_resched( conn );
1115 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1120 int connection_read(ber_socket_t s)
1125 assert( connections != NULL );
1127 ldap_pvt_thread_mutex_lock( &connections_mutex );
1129 /* get (locked) connection */
1130 c = connection_get( s );
1134 LDAP_LOG( CONNECTION, INFO,
1135 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1137 Debug( LDAP_DEBUG_ANY,
1138 "connection_read(%ld): no connection!\n",
1143 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1149 if( c->c_conn_state == SLAP_C_CLOSING ) {
1151 LDAP_LOG( CONNECTION, INFO,
1152 "connection_read: conn %lu connection closing, ignoring input\n",
1153 c->c_connid, 0, 0 );
1155 Debug( LDAP_DEBUG_TRACE,
1156 "connection_read(%d): closing, ignoring input for id=%lu\n",
1157 s, c->c_connid, 0 );
1159 connection_return( c );
1160 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1165 LDAP_LOG( CONNECTION, DETAIL1,
1166 "connection_read: conn %lu checking for input.\n",
1167 c->c_connid, 0, 0 );
1169 Debug( LDAP_DEBUG_TRACE,
1170 "connection_read(%d): checking for input on id=%lu\n",
1171 s, c->c_connid, 0 );
1175 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1176 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1178 #if 0 /* required by next #if 0 */
1184 LDAP_LOG( CONNECTION, ERR,
1185 "connection_read: conn %lu TLS accept error, error %d\n",
1186 c->c_connid, rc, 0 );
1188 Debug( LDAP_DEBUG_TRACE,
1189 "connection_read(%d): TLS accept error "
1190 "error=%d id=%lu, closing\n",
1191 s, rc, c->c_connid );
1193 c->c_needs_tls_accept = 0;
1194 /* connections_mutex and c_mutex are locked */
1195 connection_closing( c );
1198 /* Drain input before close, to allow SSL error codes
1199 * to propagate to client. */
1205 rc = select(s+1, &rfd, NULL, NULL, &tv);
1207 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1211 connection_close( c );
1213 } else if ( rc == 0 ) {
1215 struct berval authid = { 0, NULL };
1217 c->c_needs_tls_accept = 0;
1219 /* we need to let SASL know */
1220 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1222 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1223 if( c->c_tls_ssf > c->c_ssf ) {
1224 c->c_ssf = c->c_tls_ssf;
1227 rc = dnX509peerNormalize( ssl, &authid );
1228 if ( rc != LDAP_SUCCESS ) {
1230 LDAP_LOG( CONNECTION, INFO,
1231 "connection_read: conn %lu unable to get TLS client DN, "
1232 "error %d\n", c->c_connid, rc, 0 );
1234 Debug( LDAP_DEBUG_TRACE,
1235 "connection_read(%d): unable to get TLS client DN "
1236 "error=%d id=%lu\n",
1237 s, rc, c->c_connid );
1240 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1241 if ( authid.bv_val ) free( authid.bv_val );
1244 /* if success and data is ready, fall thru to data input loop */
1246 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1248 connection_return( c );
1249 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1255 #ifdef HAVE_CYRUS_SASL
1256 if ( c->c_sasl_layers ) {
1257 /* If previous layer is not removed yet, give up for now */
1258 if ( !c->c_sasl_sockctx ) {
1259 connection_return( c );
1260 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1264 c->c_sasl_layers = 0;
1266 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1268 if( rc != LDAP_SUCCESS ) {
1270 LDAP_LOG( CONNECTION, ERR,
1271 "connection_read: conn %lu SASL install error %d, closing\n",
1272 c->c_connid, rc, 0 );
1274 Debug( LDAP_DEBUG_TRACE,
1275 "connection_read(%d): SASL install error "
1276 "error=%d id=%lu, closing\n",
1277 s, rc, c->c_connid );
1279 /* connections_mutex and c_mutex are locked */
1280 connection_closing( c );
1281 connection_close( c );
1282 connection_return( c );
1283 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1289 #define CONNECTION_INPUT_LOOP 1
1290 /* #define DATA_READY_LOOP 1 */
1294 /* How do we do this without getting into a busy loop ? */
1295 rc = connection_input( c );
1297 #ifdef DATA_READY_LOOP
1298 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) );
1299 #elif CONNECTION_INPUT_LOOP
1307 LDAP_LOG( CONNECTION, ERR,
1308 "connection_read: conn %lu input error %d, closing.\n",
1309 c->c_connid, rc, 0 );
1311 Debug( LDAP_DEBUG_TRACE,
1312 "connection_read(%d): input error=%d id=%lu, closing.\n",
1313 s, rc, c->c_connid );
1315 /* connections_mutex and c_mutex are locked */
1316 connection_closing( c );
1317 connection_close( c );
1318 connection_return( c );
1319 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1323 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1324 slapd_set_read( s, 1 );
1327 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1328 slapd_set_write( s, 1 );
1331 connection_return( c );
1332 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1347 #ifdef LDAP_CONNECTIONLESS
1352 if ( conn->c_currentber == NULL &&
1353 ( conn->c_currentber = ber_alloc()) == NULL )
1356 LDAP_LOG( CONNECTION, ERR,
1357 "connection_input: conn %lu ber_alloc failed.\n",
1358 conn->c_connid, 0, 0 );
1360 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1367 #ifdef LDAP_CONNECTIONLESS
1368 if ( conn->c_is_udp ) {
1369 char peername[sizeof("IP=255.255.255.255:65336")];
1370 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1371 sizeof(struct sockaddr));
1372 if (len != sizeof(struct sockaddr))
1374 sprintf( peername, "IP=%s:%d",
1375 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1376 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1377 Statslog( LDAP_DEBUG_STATS,
1378 "conn=%lu UDP request from %s (%s) accepted.\n",
1379 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1382 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1383 if ( tag != LDAP_TAG_MESSAGE ) {
1387 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1390 LDAP_LOG( CONNECTION, ERR,
1391 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1392 conn->c_connid, err, sock_errstr(err) );
1394 Debug( LDAP_DEBUG_TRACE,
1395 "ber_get_next on fd %d failed errno=%d (%s)\n",
1396 sd, err, sock_errstr(err) );
1398 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1399 /* log, close and send error */
1400 ber_free( conn->c_currentber, 1 );
1401 conn->c_currentber = NULL;
1408 ber = conn->c_currentber;
1409 conn->c_currentber = NULL;
1411 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1412 /* log, close and send error */
1414 LDAP_LOG( CONNECTION, ERR,
1415 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1416 conn->c_connid, tag, 0 );
1418 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1425 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1426 /* log, close and send error */
1428 LDAP_LOG( CONNECTION, ERR,
1429 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1430 conn->c_connid, tag, 0 );
1432 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1440 #ifdef LDAP_CONNECTIONLESS
1441 if( conn->c_is_udp ) {
1442 if( tag == LBER_OCTETSTRING ) {
1443 ber_get_stringa( ber, &cdn );
1444 tag = ber_peek_tag(ber, &len);
1446 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1448 LDAP_LOG( CONNECTION, ERR,
1449 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1450 conn->c_connid, tag, 0 );
1452 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1460 if(tag == LDAP_REQ_BIND) {
1461 /* immediately abandon all exiting operations upon BIND */
1462 connection_abandon( conn );
1465 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1468 op->vrFilter = NULL;
1469 #ifdef LDAP_CONTROL_PAGEDRESULTS
1470 op->o_pagedresults_state = conn->c_pagedresults_state;
1472 #ifdef LDAP_CONNECTIONLESS
1473 if (conn->c_is_udp) {
1476 ber_str2bv( cdn, 0, 1, &op->o_dn );
1477 op->o_protocol = LDAP_VERSION2;
1479 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1480 if (op->o_res_ber == NULL)
1483 rc = ber_write(op->o_res_ber, (char *)&peeraddr, sizeof(struct sockaddr), 0);
1484 if (rc != sizeof(struct sockaddr)) {
1486 LDAP_LOG( CONNECTION, INFO,
1487 "connection_input: conn %lu ber_write failed\n",
1488 conn->c_connid, 0, 0 );
1490 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1495 if (op->o_protocol == LDAP_VERSION2) {
1496 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1499 LDAP_LOG( CONNECTION, INFO,
1500 "connection_input: conn %lu put outer sequence failed\n",
1501 conn->c_connid, 0, 0 );
1503 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1509 #endif /* LDAP_CONNECTIONLESS */
1513 /* Don't process requests when the conn is in the middle of a
1514 * Bind, or if it's closing. Also, don't let any single conn
1515 * use up all the available threads, and don't execute if we're
1516 * currently blocked on output. And don't execute if there are
1517 * already pending ops, let them go first.
1519 if ( conn->c_conn_state == SLAP_C_BINDING
1520 || conn->c_conn_state == SLAP_C_CLOSING
1521 || conn->c_n_ops_executing >= connection_pool_max/2
1522 || conn->c_n_ops_pending
1523 || conn->c_writewaiter)
1525 int max = conn->c_dn.bv_len ? slap_conn_max_pending_auth
1526 : slap_conn_max_pending;
1528 LDAP_LOG( CONNECTION, INFO,
1529 "connection_input: conn %lu deferring operation\n",
1530 conn->c_connid, 0, 0 );
1532 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1534 conn->c_n_ops_pending++;
1535 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1536 if ( conn->c_n_ops_pending > max ) {
1542 conn->c_n_ops_executing++;
1543 connection_op_activate( op );
1547 if ( conn->c_struct_state != SLAP_C_USED ) {
1548 /* connection must have got closed underneath us */
1552 assert( conn->c_struct_state == SLAP_C_USED );
1558 connection_resched( Connection *conn )
1562 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1565 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1567 /* us trylock to avoid possible deadlock */
1568 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1572 LDAP_LOG( CONNECTION, DETAIL1,
1573 "connection_resched: conn %lu reaquiring locks.\n",
1574 conn->c_connid, 0, 0 );
1576 Debug( LDAP_DEBUG_TRACE,
1577 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1578 conn->c_connid, sd, 0 );
1581 * reaquire locks in the right order...
1582 * this may allow another thread to close this connection,
1583 * so recheck state below.
1585 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1586 ldap_pvt_thread_mutex_lock( &connections_mutex );
1587 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1590 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1592 LDAP_LOG( CONNECTION, INFO,
1593 "connection_resched: conn %lu closed by other thread.\n",
1594 conn->c_connid, 0, 0 );
1596 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1597 "closed by other thread conn=%lu sd=%d\n",
1598 conn->c_connid, sd, 0 );
1602 LDAP_LOG( CONNECTION, DETAIL1,
1603 "connection_resched: conn %lu attempting closing.\n",
1604 conn->c_connid, 0, 0 );
1606 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1607 "attempting closing conn=%lu sd=%d\n",
1608 conn->c_connid, sd, 0 );
1610 connection_close( conn );
1613 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1617 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1618 /* other states need different handling */
1622 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1623 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1626 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1627 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1628 /* pending operations should not be marked for abandonment */
1629 assert(!op->o_abandon);
1631 conn->c_n_ops_pending--;
1632 conn->c_n_ops_executing++;
1634 connection_op_activate( op );
1636 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1643 static int connection_op_activate( Operation *op )
1646 ber_tag_t tag = op->o_tag;
1648 if(tag == LDAP_REQ_BIND) {
1649 op->o_conn->c_conn_state = SLAP_C_BINDING;
1652 if (!op->o_dn.bv_len) {
1653 op->o_authz = op->o_conn->c_authz;
1654 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1655 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1657 op->o_authtype = op->o_conn->c_authtype;
1658 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1660 if (!op->o_protocol) {
1661 op->o_protocol = op->o_conn->c_protocol
1662 ? op->o_conn->c_protocol : LDAP_VERSION3;
1664 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1665 && op->o_protocol > LDAP_VERSION2) {
1666 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1669 op->o_connid = op->o_conn->c_connid;
1671 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1673 status = ldap_pvt_thread_pool_submit( &connection_pool,
1674 connection_operation, (void *) op );
1676 if ( status != 0 ) {
1678 LDAP_LOG( CONNECTION, ERR,
1679 "connection_op_activate: conn %lu thread pool submit failed.\n",
1680 op->o_connid, 0, 0 );
1682 Debug( LDAP_DEBUG_ANY,
1683 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1685 /* should move op to pending list */
1691 int connection_write(ber_socket_t s)
1695 assert( connections != NULL );
1697 ldap_pvt_thread_mutex_lock( &connections_mutex );
1699 c = connection_get( s );
1701 slapd_clr_write( s, 0);
1705 LDAP_LOG( CONNECTION, ERR,
1706 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1708 Debug( LDAP_DEBUG_ANY,
1709 "connection_write(%ld): no connection!\n",
1713 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1720 LDAP_LOG( CONNECTION, DETAIL1,
1721 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1723 Debug( LDAP_DEBUG_TRACE,
1724 "connection_write(%d): waking output for id=%lu\n",
1725 s, c->c_connid, 0 );
1727 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1729 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1730 slapd_set_read( s, 1 );
1731 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1732 slapd_set_write( s, 1 );
1733 connection_return( c );
1734 ldap_pvt_thread_mutex_unlock( &connections_mutex );