3 * Copyright 1998-2002 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( Connection *conn, 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;
71 * Initialize connection management infrastructure.
73 int connections_init(void)
75 assert( connections == NULL );
77 if( connections != NULL) {
79 LDAP_LOG( CONNECTION, INFO,
80 "connections_init: already initialized.\n", 0, 0, 0 );
82 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
88 /* should check return of every call */
89 ldap_pvt_thread_mutex_init( &connections_mutex );
91 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
93 if( connections == NULL ) {
95 LDAP_LOG( CONNECTION, ERR,
96 "connections_init: allocation (%d * %ld) of connection "
97 "array failed\n", dtblsize, (long) sizeof(Connection), 0 );
99 Debug( LDAP_DEBUG_ANY,
100 "connections_init: allocation (%d*%ld) of connection array failed\n",
101 dtblsize, (long) sizeof(Connection), 0 );
106 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
107 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
110 * per entry initialization of the Connection array initialization
111 * will be done by connection_init()
118 * Destroy connection management infrastructure.
120 int connections_destroy(void)
124 /* should check return of every call */
126 if( connections == NULL) {
128 LDAP_LOG( CONNECTION, INFO,
129 "connections_destroy: nothing to destroy.\n", 0, 0, 0 );
131 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
137 for ( i = 0; i < dtblsize; i++ ) {
138 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
139 ber_sockbuf_free( connections[i].c_sb );
140 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
141 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
142 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
149 ldap_pvt_thread_mutex_destroy( &connections_mutex );
154 * shutdown all connections
156 int connections_shutdown(void)
160 ldap_pvt_thread_mutex_lock( &connections_mutex );
162 for ( i = 0; i < dtblsize; i++ ) {
163 if( connections[i].c_struct_state != SLAP_C_USED ) {
167 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
169 /* connections_mutex and c_mutex are locked */
170 connection_closing( &connections[i] );
171 connection_close( &connections[i] );
173 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
176 ldap_pvt_thread_mutex_unlock( &connections_mutex );
182 * Timeout idle connections.
184 int connections_timeout_idle(time_t now)
190 for( c = connection_first( &connindex );
192 c = connection_next( c, &connindex ) )
194 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
196 connection_closing( c );
197 connection_close( c );
201 connection_done( c );
206 static Connection* connection_get( ber_socket_t s )
208 /* connections_mutex should be locked by caller */
213 LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
215 Debug( LDAP_DEBUG_ARGS,
216 "connection_get(%ld)\n",
220 assert( connections != NULL );
222 if(s == AC_SOCKET_INVALID) {
229 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
236 for(i=0; i<dtblsize; i++) {
237 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
238 assert( connections[i].c_conn_state == SLAP_C_INVALID );
239 assert( connections[i].c_sb == 0 );
243 ber_sockbuf_ctrl( connections[i].c_sb,
244 LBER_SB_OPT_GET_FD, &sd );
246 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
247 assert( connections[i].c_conn_state == SLAP_C_INVALID );
248 assert( sd == AC_SOCKET_INVALID );
252 /* state can actually change from used -> unused by resched,
253 * so don't assert details here.
267 ldap_pvt_thread_mutex_lock( &c->c_mutex );
269 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
270 if( c->c_struct_state != SLAP_C_USED ) {
271 /* connection must have been closed due to resched */
273 assert( c->c_conn_state == SLAP_C_INVALID );
274 assert( sd == AC_SOCKET_INVALID );
277 LDAP_LOG( CONNECTION, ARGS,
278 "connection_get: connection %d not used\n", s, 0, 0 );
280 Debug( LDAP_DEBUG_TRACE,
281 "connection_get(%d): connection not used\n",
285 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
290 LDAP_LOG( CONNECTION, RESULTS,
291 "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
293 Debug( LDAP_DEBUG_TRACE,
294 "connection_get(%d): got connid=%lu\n",
300 assert( c->c_struct_state == SLAP_C_USED );
301 assert( c->c_conn_state != SLAP_C_INVALID );
302 assert( sd != AC_SOCKET_INVALID );
305 c->c_activitytime = slap_get_time();
307 if( global_idletimeout > 0 ) {
308 c->c_activitytime = slap_get_time();
316 static void connection_return( Connection *c )
318 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
321 long connection_init(
325 const char* peername,
333 assert( connections != NULL );
335 assert( listener != NULL );
336 assert( dnsname != NULL );
337 assert( peername != NULL );
340 assert( tls_udp_option != 1 );
343 if( s == AC_SOCKET_INVALID ) {
345 LDAP_LOG( CONNECTION, INFO,
346 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
348 Debug( LDAP_DEBUG_ANY,
349 "connection_init(%ld): invalid.\n",
357 assert( s < dtblsize );
360 ldap_pvt_thread_mutex_lock( &connections_mutex );
371 for( i=0; i < dtblsize; i++) {
374 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
375 assert( connections[i].c_sb == 0 );
380 sd = AC_SOCKET_INVALID;
381 if (connections[i].c_sb != NULL)
382 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
384 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
385 assert( sd == AC_SOCKET_INVALID );
390 assert( connections[i].c_struct_state == SLAP_C_USED );
391 assert( connections[i].c_conn_state != SLAP_C_INVALID );
392 assert( sd != AC_SOCKET_INVALID );
397 LDAP_LOG( CONNECTION, INFO,
398 "connection_init: skt %d connection table full (%d/%d)\n",
401 Debug( LDAP_DEBUG_ANY,
402 "connection_init(%d): connection table full (%d/%d)\n",
405 ldap_pvt_thread_mutex_unlock( &connections_mutex );
413 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
414 c->c_authmech.bv_val = NULL;
415 c->c_authmech.bv_len = 0;
416 c->c_dn.bv_val = NULL;
418 c->c_ndn.bv_val = NULL;
422 c->c_listener = NULL;
423 c->c_peer_domain.bv_val = NULL;
424 c->c_peer_domain.bv_len = 0;
425 c->c_peer_name.bv_val = NULL;
426 c->c_peer_name.bv_len = 0;
428 LDAP_STAILQ_INIT(&c->c_ops);
429 LDAP_STAILQ_INIT(&c->c_pending_ops);
431 c->c_sasl_bind_mech.bv_val = NULL;
432 c->c_sasl_bind_mech.bv_len = 0;
433 c->c_sasl_context = NULL;
434 c->c_sasl_extra = NULL;
435 c->c_sasl_bindop = NULL;
437 c->c_sb = ber_sockbuf_alloc( );
440 ber_len_t max = sockbuf_max_incoming;
441 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
444 c->c_currentber = NULL;
446 /* should check status of thread calls */
447 ldap_pvt_thread_mutex_init( &c->c_mutex );
448 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
449 ldap_pvt_thread_cond_init( &c->c_write_cv );
451 c->c_struct_state = SLAP_C_UNUSED;
454 ldap_pvt_thread_mutex_lock( &c->c_mutex );
456 assert( c->c_struct_state == SLAP_C_UNUSED );
457 assert( c->c_authmech.bv_val == NULL );
458 assert( c->c_dn.bv_val == NULL );
459 assert( c->c_ndn.bv_val == NULL );
460 assert( c->c_groups == NULL );
461 assert( c->c_listener == NULL );
462 assert( c->c_peer_domain.bv_val == NULL );
463 assert( c->c_peer_name.bv_val == NULL );
464 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
465 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
466 assert( c->c_sasl_bind_mech.bv_val == NULL );
467 assert( c->c_sasl_context == NULL );
468 assert( c->c_sasl_extra == NULL );
469 assert( c->c_sasl_bindop == NULL );
470 assert( c->c_currentber == NULL );
472 c->c_listener = listener;
473 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
474 ber_str2bv( peername, 0, 1, &c->c_peer_name );
476 c->c_n_ops_received = 0;
477 c->c_n_ops_executing = 0;
478 c->c_n_ops_pending = 0;
479 c->c_n_ops_completed = 0;
485 /* set to zero until bind, implies LDAP_VERSION3 */
489 c->c_activitytime = c->c_starttime = slap_get_time();
491 if( global_idletimeout > 0 ) {
492 c->c_activitytime = c->c_starttime = slap_get_time();
496 #ifdef LDAP_CONNECTIONLESS
498 if( tls_udp_option == 2 ) {
501 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
502 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
504 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
505 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
506 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
507 LBER_SBIOD_LEVEL_PROVIDER, NULL );
512 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
513 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
515 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
516 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
520 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
521 INT_MAX, (void*)"ldap_" );
524 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
525 c /* non-NULL */ ) < 0 )
528 LDAP_LOG( CONNECTION, INFO,
529 "connection_init: conn %lu set nonblocking failed\n",
532 Debug( LDAP_DEBUG_ANY,
533 "connection_init(%d, %s): set nonblocking failed\n",
534 s, c->c_peer_name.bv_val, 0 );
538 id = c->c_connid = conn_nextid++;
540 c->c_conn_state = SLAP_C_INACTIVE;
541 c->c_struct_state = SLAP_C_USED;
543 c->c_ssf = c->c_transport_ssf = ssf;
547 if ( tls_udp_option == 1 ) {
549 c->c_needs_tls_accept = 1;
552 c->c_needs_tls_accept = 0;
557 slap_sasl_external( c, ssf, authid );
559 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
560 ldap_pvt_thread_mutex_unlock( &connections_mutex );
562 backend_connection_init(c);
567 void connection2anonymous( Connection *c )
569 assert( connections != NULL );
573 ber_len_t max = sockbuf_max_incoming;
574 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
577 if(c->c_authmech.bv_val != NULL ) {
578 free(c->c_authmech.bv_val);
579 c->c_authmech.bv_val = NULL;
581 c->c_authmech.bv_len = 0;
583 if(c->c_dn.bv_val != NULL) {
584 free(c->c_dn.bv_val);
585 c->c_dn.bv_val = NULL;
588 if(c->c_ndn.bv_val != NULL) {
589 free(c->c_ndn.bv_val);
590 c->c_ndn.bv_val = NULL;
594 c->c_authz_backend = NULL;
597 GroupAssertion *g, *n;
598 for (g = c->c_groups; g; g=n) {
607 connection_destroy( Connection *c )
609 /* note: connections_mutex should be locked by caller */
611 unsigned long connid;
613 assert( connections != NULL );
615 assert( c->c_struct_state != SLAP_C_UNUSED );
616 assert( c->c_conn_state != SLAP_C_INVALID );
617 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
619 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
620 connid = c->c_connid;
622 backend_connection_destroy(c);
627 c->c_activitytime = c->c_starttime = 0;
629 connection2anonymous( c );
630 c->c_listener = NULL;
632 if(c->c_peer_domain.bv_val != NULL) {
633 free(c->c_peer_domain.bv_val);
634 c->c_peer_domain.bv_val = NULL;
636 c->c_peer_domain.bv_len = 0;
637 if(c->c_peer_name.bv_val != NULL) {
638 free(c->c_peer_name.bv_val);
639 c->c_peer_name.bv_val = NULL;
641 c->c_peer_name.bv_len = 0;
643 c->c_sasl_bind_in_progress = 0;
644 if(c->c_sasl_bind_mech.bv_val != NULL) {
645 free(c->c_sasl_bind_mech.bv_val);
646 c->c_sasl_bind_mech.bv_val = NULL;
648 c->c_sasl_bind_mech.bv_len = 0;
650 slap_sasl_close( c );
652 if ( c->c_currentber != NULL ) {
653 ber_free( c->c_currentber, 1 );
654 c->c_currentber = NULL;
657 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
658 if ( sd != AC_SOCKET_INVALID ) {
659 slapd_remove( sd, 0 );
661 Statslog( LDAP_DEBUG_STATS,
662 "conn=%lu fd=%d closed\n",
663 connid, sd, 0, 0, 0 );
666 ber_sockbuf_free( c->c_sb );
668 c->c_sb = ber_sockbuf_alloc( );
671 ber_len_t max = sockbuf_max_incoming;
672 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
675 c->c_conn_state = SLAP_C_INVALID;
676 c->c_struct_state = SLAP_C_UNUSED;
679 int connection_state_closing( Connection *c )
681 /* c_mutex must be locked by caller */
685 assert( c->c_struct_state == SLAP_C_USED );
687 state = c->c_conn_state;
689 assert( state != SLAP_C_INVALID );
691 return state == SLAP_C_CLOSING;
694 static void connection_abandon( Connection *c )
696 /* c_mutex must be locked by caller */
700 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
704 /* remove pending operations */
705 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
706 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
707 LDAP_STAILQ_NEXT(o, o_next) = NULL;
712 void connection_closing( Connection *c )
714 assert( connections != NULL );
716 assert( c->c_struct_state == SLAP_C_USED );
717 assert( c->c_conn_state != SLAP_C_INVALID );
719 /* c_mutex must be locked by caller */
721 if( c->c_conn_state != SLAP_C_CLOSING ) {
724 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
726 LDAP_LOG( CONNECTION, DETAIL1,
727 "connection_closing: conn %lu readying socket %d for close.\n",
728 c->c_connid, sd, 0 );
730 Debug( LDAP_DEBUG_TRACE,
731 "connection_closing: readying conn=%lu sd=%d for close\n",
732 c->c_connid, sd, 0 );
734 /* update state to closing */
735 c->c_conn_state = SLAP_C_CLOSING;
737 /* don't listen on this port anymore */
738 slapd_clr_read( sd, 1 );
740 /* abandon active operations */
741 connection_abandon( c );
743 /* wake write blocked operations */
744 slapd_clr_write( sd, 1 );
745 ldap_pvt_thread_cond_signal( &c->c_write_cv );
749 static void connection_close( Connection *c )
753 assert( connections != NULL );
755 assert( c->c_struct_state == SLAP_C_USED );
756 assert( c->c_conn_state == SLAP_C_CLOSING );
758 /* note: connections_mutex and c_mutex should be locked by caller */
760 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
761 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
763 LDAP_LOG( CONNECTION, DETAIL1,
764 "connection_close: conn %lu deferring sd %d\n",
765 c->c_connid, sd, 0 );
767 Debug( LDAP_DEBUG_TRACE,
768 "connection_close: deferring conn=%lu sd=%d\n",
769 c->c_connid, sd, 0 );
775 LDAP_LOG( CONNECTION, RESULTS,
776 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
778 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
779 c->c_connid, sd, 0 );
781 connection_destroy( c );
784 unsigned long connections_nextid(void)
787 assert( connections != NULL );
789 ldap_pvt_thread_mutex_lock( &connections_mutex );
793 ldap_pvt_thread_mutex_unlock( &connections_mutex );
798 Connection* connection_first( ber_socket_t *index )
800 assert( connections != NULL );
801 assert( index != NULL );
803 ldap_pvt_thread_mutex_lock( &connections_mutex );
807 return connection_next(NULL, index);
810 Connection* connection_next( Connection *c, ber_socket_t *index )
812 assert( connections != NULL );
813 assert( index != NULL );
814 assert( *index <= dtblsize );
817 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
822 for(; *index < dtblsize; (*index)++) {
823 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
824 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
832 if( connections[*index].c_struct_state == SLAP_C_USED ) {
833 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
834 c = &connections[(*index)++];
838 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
839 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
843 ldap_pvt_thread_mutex_lock( &c->c_mutex );
849 void connection_done( Connection *c )
851 assert( connections != NULL );
854 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
857 ldap_pvt_thread_mutex_unlock( &connections_mutex );
861 * connection_activity - handle the request operation op on connection
862 * conn. This routine figures out what kind of operation it is and
863 * calls the appropriate stub to handle it.
867 #define INCR_OP(var,index) \
869 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
871 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
873 #else /* !SLAPD_MONITOR */
874 #define INCR_OP(var,index)
875 #endif /* !SLAPD_MONITOR */
878 connection_operation( void *ctx, void *arg_v )
881 struct co_arg *arg = arg_v;
882 ber_tag_t tag = arg->co_op->o_tag;
884 ber_tag_t oldtag = tag;
885 #endif /* SLAPD_MONITOR */
886 Connection *conn = arg->co_conn;
888 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
890 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
892 arg->co_op->o_threadctx = ctx;
894 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
896 LDAP_LOG( CONNECTION, ERR,
897 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
898 conn->c_connid, (long)tag, 0 );
900 Debug( LDAP_DEBUG_ANY, "connection_operation: "
901 "error: SASL bind in progress (tag=%ld).\n",
904 send_ldap_result( conn, arg->co_op,
905 rc = LDAP_OPERATIONS_ERROR,
906 NULL, "SASL bind in progress", NULL, NULL );
907 goto operations_error;
912 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
913 rc = do_bind( conn, arg->co_op );
916 case LDAP_REQ_UNBIND:
917 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
918 rc = do_unbind( conn, arg->co_op );
922 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
923 rc = do_add( conn, arg->co_op );
926 case LDAP_REQ_DELETE:
927 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
928 rc = do_delete( conn, arg->co_op );
931 case LDAP_REQ_MODRDN:
932 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
933 rc = do_modrdn( conn, arg->co_op );
936 case LDAP_REQ_MODIFY:
937 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
938 rc = do_modify( conn, arg->co_op );
941 case LDAP_REQ_COMPARE:
942 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
943 rc = do_compare( conn, arg->co_op );
946 case LDAP_REQ_SEARCH:
947 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
948 rc = do_search( conn, arg->co_op );
951 case LDAP_REQ_ABANDON:
952 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
953 rc = do_abandon( conn, arg->co_op );
956 case LDAP_REQ_EXTENDED:
957 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
958 rc = do_extended( conn, arg->co_op );
963 LDAP_LOG( CONNECTION, INFO,
964 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
965 conn->c_connid, tag, 0 );
967 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
970 arg->co_op->o_tag = LBER_ERROR;
971 send_ldap_disconnect( conn, arg->co_op,
972 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
979 #endif /* SLAPD_MONITOR */
980 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
983 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
988 num_ops_completed_[SLAP_OP_BIND]++;
990 case LDAP_REQ_UNBIND:
991 num_ops_completed_[SLAP_OP_UNBIND]++;
994 num_ops_completed_[SLAP_OP_ADD]++;
996 case LDAP_REQ_DELETE:
997 num_ops_completed_[SLAP_OP_DELETE]++;
999 case LDAP_REQ_MODRDN:
1000 num_ops_completed_[SLAP_OP_MODRDN]++;
1002 case LDAP_REQ_MODIFY:
1003 num_ops_completed_[SLAP_OP_MODIFY]++;
1005 case LDAP_REQ_COMPARE:
1006 num_ops_completed_[SLAP_OP_COMPARE]++;
1008 case LDAP_REQ_SEARCH:
1009 num_ops_completed_[SLAP_OP_SEARCH]++;
1011 case LDAP_REQ_ABANDON:
1012 num_ops_completed_[SLAP_OP_ABANDON]++;
1014 case LDAP_REQ_EXTENDED:
1015 num_ops_completed_[SLAP_OP_EXTENDED]++;
1018 #endif /* SLAPD_MONITOR */
1019 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1021 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1023 conn->c_n_ops_executing--;
1024 conn->c_n_ops_completed++;
1026 LDAP_STAILQ_REMOVE( &conn->c_ops, arg->co_op, slap_op, o_next);
1027 LDAP_STAILQ_NEXT(arg->co_op, o_next) = NULL;
1028 #ifdef LDAP_CLIENT_UPDATE
1029 if ( !( arg->co_op->o_clientupdate_type & SLAP_LCUP_PERSIST ) )
1030 #endif /* LDAP_CLIENT_UPDATE */
1032 slap_op_free( arg->co_op );
1035 arg->co_conn = NULL;
1036 free( (char *) arg );
1041 case LDAP_REQ_UNBIND:
1042 /* c_mutex is locked */
1043 connection_closing( conn );
1047 conn->c_sasl_bind_in_progress =
1048 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1050 if( conn->c_conn_state == SLAP_C_BINDING) {
1051 conn->c_conn_state = SLAP_C_ACTIVE;
1055 connection_resched( conn );
1057 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1062 int connection_read(ber_socket_t s)
1067 assert( connections != NULL );
1069 ldap_pvt_thread_mutex_lock( &connections_mutex );
1071 /* get (locked) connection */
1072 c = connection_get( s );
1076 LDAP_LOG( CONNECTION, INFO,
1077 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1079 Debug( LDAP_DEBUG_ANY,
1080 "connection_read(%ld): no connection!\n",
1085 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1091 if( c->c_conn_state == SLAP_C_CLOSING ) {
1093 LDAP_LOG( CONNECTION, INFO,
1094 "connection_read: conn %lu connection closing, ignoring input\n",
1095 c->c_connid, 0, 0 );
1097 Debug( LDAP_DEBUG_TRACE,
1098 "connection_read(%d): closing, ignoring input for id=%lu\n",
1099 s, c->c_connid, 0 );
1101 connection_return( c );
1102 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1107 LDAP_LOG( CONNECTION, DETAIL1,
1108 "connection_read: conn %lu checking for input.\n",
1109 c->c_connid, 0, 0 );
1111 Debug( LDAP_DEBUG_TRACE,
1112 "connection_read(%d): checking for input on id=%lu\n",
1113 s, c->c_connid, 0 );
1117 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1118 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1120 #if 0 /* required by next #if 0 */
1126 LDAP_LOG( CONNECTION, ERR,
1127 "connection_read: conn %lu TLS accept error, error %d\n",
1128 c->c_connid, rc, 0 );
1130 Debug( LDAP_DEBUG_TRACE,
1131 "connection_read(%d): TLS accept error "
1132 "error=%d id=%lu, closing\n",
1133 s, rc, c->c_connid );
1135 c->c_needs_tls_accept = 0;
1136 /* connections_mutex and c_mutex are locked */
1137 connection_closing( c );
1140 /* Drain input before close, to allow SSL error codes
1141 * to propagate to client. */
1147 rc = select(s+1, &rfd, NULL, NULL, &tv);
1149 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1153 connection_close( c );
1155 } else if ( rc == 0 ) {
1157 struct berval authid = { 0, NULL };
1159 c->c_needs_tls_accept = 0;
1161 /* we need to let SASL know */
1162 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1164 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1165 if( c->c_tls_ssf > c->c_ssf ) {
1166 c->c_ssf = c->c_tls_ssf;
1169 rc = dnX509peerNormalize( ssl, &authid );
1170 if ( rc != LDAP_SUCCESS ) {
1172 LDAP_LOG( CONNECTION, INFO,
1173 "connection_read: conn %lu unable to get TLS client DN, "
1174 "error %d\n", c->c_connid, rc, 0 );
1176 Debug( LDAP_DEBUG_TRACE,
1177 "connection_read(%d): unable to get TLS client DN "
1178 "error=%d id=%lu\n",
1179 s, rc, c->c_connid );
1182 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1183 if ( authid.bv_val ) free( authid.bv_val );
1186 /* if success and data is ready, fall thru to data input loop */
1188 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1190 connection_return( c );
1191 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1197 #ifdef HAVE_CYRUS_SASL
1198 if ( c->c_sasl_layers ) {
1199 c->c_sasl_layers = 0;
1201 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1203 if( rc != LDAP_SUCCESS ) {
1205 LDAP_LOG( CONNECTION, ERR,
1206 "connection_read: conn %lu SASL install error %d, closing\n",
1207 c->c_connid, rc, 0 );
1209 Debug( LDAP_DEBUG_TRACE,
1210 "connection_read(%d): SASL install error "
1211 "error=%d id=%lu, closing\n",
1212 s, rc, c->c_connid );
1214 /* connections_mutex and c_mutex are locked */
1215 connection_closing( c );
1216 connection_close( c );
1217 connection_return( c );
1218 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1224 #define CONNECTION_INPUT_LOOP 1
1226 #ifdef DATA_READY_LOOP
1227 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1228 #elif CONNECTION_INPUT_LOOP
1232 /* How do we do this without getting into a busy loop ? */
1233 rc = connection_input( c );
1238 LDAP_LOG( CONNECTION, ERR,
1239 "connection_read: conn %lu input error %d, closing.\n",
1240 c->c_connid, rc, 0 );
1242 Debug( LDAP_DEBUG_TRACE,
1243 "connection_read(%d): input error=%d id=%lu, closing.\n",
1244 s, rc, c->c_connid );
1246 /* connections_mutex and c_mutex are locked */
1247 connection_closing( c );
1248 connection_close( c );
1249 connection_return( c );
1250 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1254 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1255 slapd_set_read( s, 1 );
1258 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1259 slapd_set_write( s, 1 );
1262 connection_return( c );
1263 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1277 #ifdef LDAP_CONNECTIONLESS
1282 if ( conn->c_currentber == NULL &&
1283 ( conn->c_currentber = ber_alloc()) == NULL )
1286 LDAP_LOG( CONNECTION, ERR,
1287 "connection_input: conn %lu ber_alloc failed.\n",
1288 conn->c_connid, 0, 0 );
1290 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1297 #ifdef LDAP_CONNECTIONLESS
1298 if ( conn->c_is_udp ) {
1299 char peername[sizeof("IP=255.255.255.255:65336")];
1300 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1301 sizeof(struct sockaddr));
1302 if (len != sizeof(struct sockaddr))
1304 sprintf( peername, "IP=%s:%d",
1305 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1306 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1307 Statslog( LDAP_DEBUG_STATS,
1308 "conn=%lu UDP request from %s (%s) accepted.\n",
1309 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1312 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1313 if ( tag != LDAP_TAG_MESSAGE ) {
1317 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1320 LDAP_LOG( CONNECTION, ERR,
1321 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1322 conn->c_connid, err, sock_errstr(err) );
1324 Debug( LDAP_DEBUG_TRACE,
1325 "ber_get_next on fd %d failed errno=%d (%s)\n",
1326 sd, err, sock_errstr(err) );
1328 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1329 /* log, close and send error */
1330 ber_free( conn->c_currentber, 1 );
1331 conn->c_currentber = NULL;
1338 ber = conn->c_currentber;
1339 conn->c_currentber = NULL;
1341 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1342 /* log, close and send error */
1344 LDAP_LOG( CONNECTION, ERR,
1345 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1346 conn->c_connid, tag, 0 );
1348 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1355 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1356 /* log, close and send error */
1358 LDAP_LOG( CONNECTION, ERR,
1359 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1360 conn->c_connid, tag, 0 );
1362 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1370 #ifdef LDAP_CONNECTIONLESS
1371 if( conn->c_is_udp ) {
1372 if( tag == LBER_OCTETSTRING ) {
1373 ber_get_stringa( ber, &cdn );
1374 tag = ber_peek_tag(ber, &len);
1376 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1378 LDAP_LOG( CONNECTION, ERR,
1379 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1380 conn->c_connid, tag, 0 );
1382 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1390 if(tag == LDAP_REQ_BIND) {
1391 /* immediately abandon all exiting operations upon BIND */
1392 connection_abandon( conn );
1395 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1398 op->vrFilter = NULL;
1399 op->o_pagedresults_state = conn->c_pagedresults_state;
1400 #ifdef LDAP_CONNECTIONLESS
1401 op->o_peeraddr = peeraddr;
1403 ber_str2bv( cdn, 0, 1, &op->o_dn );
1404 op->o_protocol = LDAP_VERSION2;
1408 if ( conn->c_conn_state == SLAP_C_BINDING
1409 || conn->c_conn_state == SLAP_C_CLOSING )
1412 LDAP_LOG( CONNECTION, INFO,
1413 "connection_input: conn %lu deferring operation\n",
1414 conn->c_connid, 0, 0 );
1416 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1418 conn->c_n_ops_pending++;
1419 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1422 conn->c_n_ops_executing++;
1423 connection_op_activate( conn, op );
1427 if ( conn->c_struct_state != SLAP_C_USED ) {
1428 /* connection must have got closed underneath us */
1432 assert( conn->c_struct_state == SLAP_C_USED );
1438 connection_resched( Connection *conn )
1442 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1445 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1447 /* us trylock to avoid possible deadlock */
1448 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1452 LDAP_LOG( CONNECTION, DETAIL1,
1453 "connection_resched: conn %lu reaquiring locks.\n",
1454 conn->c_connid, 0, 0 );
1456 Debug( LDAP_DEBUG_TRACE,
1457 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1458 conn->c_connid, sd, 0 );
1461 * reaquire locks in the right order...
1462 * this may allow another thread to close this connection,
1463 * so recheck state below.
1465 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1466 ldap_pvt_thread_mutex_lock( &connections_mutex );
1467 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1470 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1472 LDAP_LOG( CONNECTION, INFO,
1473 "connection_resched: conn %lu closed by other thread.\n",
1474 conn->c_connid, 0, 0 );
1476 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1477 "closed by other thread conn=%lu sd=%d\n",
1478 conn->c_connid, sd, 0 );
1482 LDAP_LOG( CONNECTION, DETAIL1,
1483 "connection_resched: conn %lu attempting closing.\n",
1484 conn->c_connid, 0, 0 );
1486 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1487 "attempting closing conn=%lu sd=%d\n",
1488 conn->c_connid, sd, 0 );
1490 connection_close( conn );
1493 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1497 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1498 /* other states need different handling */
1502 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1503 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1504 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1505 /* pending operations should not be marked for abandonment */
1506 assert(!op->o_abandon);
1508 conn->c_n_ops_pending--;
1509 conn->c_n_ops_executing++;
1511 connection_op_activate( conn, op );
1513 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1520 static int connection_op_activate( Connection *conn, Operation *op )
1524 ber_tag_t tag = op->o_tag;
1526 if(tag == LDAP_REQ_BIND) {
1527 conn->c_conn_state = SLAP_C_BINDING;
1530 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1531 arg->co_conn = conn;
1534 if (!arg->co_op->o_dn.bv_len) {
1535 arg->co_op->o_authz = conn->c_authz;
1536 arg->co_op->o_dn.bv_val = ch_strdup( conn->c_dn.bv_val ?
1537 conn->c_dn.bv_val : "" );
1538 arg->co_op->o_ndn.bv_val = ch_strdup( conn->c_ndn.bv_val ?
1539 conn->c_ndn.bv_val : "" );
1541 arg->co_op->o_authtype = conn->c_authtype;
1542 ber_dupbv( &arg->co_op->o_authmech, &conn->c_authmech );
1544 if (!arg->co_op->o_protocol) {
1545 arg->co_op->o_protocol = conn->c_protocol
1546 ? conn->c_protocol : LDAP_VERSION3;
1548 arg->co_op->o_connid = conn->c_connid;
1550 LDAP_STAILQ_INSERT_TAIL( &conn->c_ops, arg->co_op, o_next );
1552 status = ldap_pvt_thread_pool_submit( &connection_pool,
1553 connection_operation, (void *) arg );
1555 if ( status != 0 ) {
1557 LDAP_LOG( CONNECTION, ERR,
1558 "connection_op_activate: conn %lu thread pool submit failed.\n",
1559 conn->c_connid, 0, 0 );
1561 Debug( LDAP_DEBUG_ANY,
1562 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1564 /* should move op to pending list */
1570 int connection_write(ber_socket_t s)
1574 assert( connections != NULL );
1576 ldap_pvt_thread_mutex_lock( &connections_mutex );
1578 c = connection_get( s );
1580 slapd_clr_write( s, 0);
1584 LDAP_LOG( CONNECTION, ERR,
1585 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1587 Debug( LDAP_DEBUG_ANY,
1588 "connection_write(%ld): no connection!\n",
1592 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1599 LDAP_LOG( CONNECTION, DETAIL1,
1600 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1602 Debug( LDAP_DEBUG_TRACE,
1603 "connection_write(%d): waking output for id=%lu\n",
1604 s, c->c_connid, 0 );
1606 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1608 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1609 slapd_set_read( s, 1 );
1610 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1611 slapd_set_write( s, 1 );
1612 connection_return( c );
1613 ldap_pvt_thread_mutex_unlock( &connections_mutex );