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( 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 "
399 "(%d/%d)\n", s, i, dtblsize );
401 Debug( LDAP_DEBUG_ANY,
402 "connection_init(%d): connection table full "
403 "(%d/%d)\n", s, i, dtblsize);
405 ldap_pvt_thread_mutex_unlock( &connections_mutex );
413 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
414 c->c_send_ldap_result = slap_send_ldap_result;
415 c->c_send_search_entry = slap_send_search_entry;
416 c->c_send_search_result = slap_send_search_result;
417 c->c_send_search_reference = slap_send_search_reference;
418 c->c_send_ldap_extended = slap_send_ldap_extended;
419 c->c_send_ldap_intermediate_resp = slap_send_ldap_intermediate_resp;
421 c->c_authmech.bv_val = NULL;
422 c->c_authmech.bv_len = 0;
423 c->c_dn.bv_val = NULL;
425 c->c_ndn.bv_val = NULL;
429 c->c_listener = NULL;
430 c->c_peer_domain.bv_val = NULL;
431 c->c_peer_domain.bv_len = 0;
432 c->c_peer_name.bv_val = NULL;
433 c->c_peer_name.bv_len = 0;
435 LDAP_STAILQ_INIT(&c->c_ops);
436 LDAP_STAILQ_INIT(&c->c_pending_ops);
438 c->c_sasl_bind_mech.bv_val = NULL;
439 c->c_sasl_bind_mech.bv_len = 0;
440 c->c_sasl_context = NULL;
441 c->c_sasl_extra = NULL;
442 c->c_sasl_bindop = NULL;
444 c->c_sb = ber_sockbuf_alloc( );
447 ber_len_t max = sockbuf_max_incoming;
448 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
451 c->c_currentber = NULL;
453 /* should check status of thread calls */
454 ldap_pvt_thread_mutex_init( &c->c_mutex );
455 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
456 ldap_pvt_thread_cond_init( &c->c_write_cv );
458 c->c_struct_state = SLAP_C_UNUSED;
461 ldap_pvt_thread_mutex_lock( &c->c_mutex );
463 assert( c->c_struct_state == SLAP_C_UNUSED );
464 assert( c->c_authmech.bv_val == NULL );
465 assert( c->c_dn.bv_val == NULL );
466 assert( c->c_ndn.bv_val == NULL );
467 assert( c->c_groups == NULL );
468 assert( c->c_listener == NULL );
469 assert( c->c_peer_domain.bv_val == NULL );
470 assert( c->c_peer_name.bv_val == NULL );
471 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
472 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
473 assert( c->c_sasl_bind_mech.bv_val == NULL );
474 assert( c->c_sasl_context == 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;
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=%d closed\n",
670 connid, 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 )
888 struct co_arg *arg = arg_v;
889 ber_tag_t tag = arg->co_op->o_tag;
891 ber_tag_t oldtag = tag;
892 #endif /* SLAPD_MONITOR */
893 Connection *conn = arg->co_conn;
895 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
897 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
899 arg->co_op->o_threadctx = ctx;
901 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
903 LDAP_LOG( CONNECTION, ERR,
904 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
905 conn->c_connid, (long)tag, 0 );
907 Debug( LDAP_DEBUG_ANY, "connection_operation: "
908 "error: SASL bind in progress (tag=%ld).\n",
911 send_ldap_result( conn, arg->co_op,
912 rc = LDAP_OPERATIONS_ERROR,
913 NULL, "SASL bind in progress", NULL, NULL );
914 goto operations_error;
919 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
920 rc = do_bind( conn, arg->co_op );
923 case LDAP_REQ_UNBIND:
924 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
925 rc = do_unbind( conn, arg->co_op );
929 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
930 rc = do_add( conn, arg->co_op );
933 case LDAP_REQ_DELETE:
934 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
935 rc = do_delete( conn, arg->co_op );
938 case LDAP_REQ_MODRDN:
939 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
940 rc = do_modrdn( conn, arg->co_op );
943 case LDAP_REQ_MODIFY:
944 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
945 rc = do_modify( conn, arg->co_op );
948 case LDAP_REQ_COMPARE:
949 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
950 rc = do_compare( conn, arg->co_op );
953 case LDAP_REQ_SEARCH:
954 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
955 rc = do_search( conn, arg->co_op );
958 case LDAP_REQ_ABANDON:
959 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
960 rc = do_abandon( conn, arg->co_op );
963 case LDAP_REQ_EXTENDED:
964 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
965 rc = do_extended( conn, arg->co_op );
970 LDAP_LOG( CONNECTION, INFO,
971 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
972 conn->c_connid, tag, 0 );
974 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
977 arg->co_op->o_tag = LBER_ERROR;
978 send_ldap_disconnect( conn, arg->co_op,
979 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
986 #endif /* SLAPD_MONITOR */
987 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
990 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
995 num_ops_completed_[SLAP_OP_BIND]++;
997 case LDAP_REQ_UNBIND:
998 num_ops_completed_[SLAP_OP_UNBIND]++;
1001 num_ops_completed_[SLAP_OP_ADD]++;
1003 case LDAP_REQ_DELETE:
1004 num_ops_completed_[SLAP_OP_DELETE]++;
1006 case LDAP_REQ_MODRDN:
1007 num_ops_completed_[SLAP_OP_MODRDN]++;
1009 case LDAP_REQ_MODIFY:
1010 num_ops_completed_[SLAP_OP_MODIFY]++;
1012 case LDAP_REQ_COMPARE:
1013 num_ops_completed_[SLAP_OP_COMPARE]++;
1015 case LDAP_REQ_SEARCH:
1016 num_ops_completed_[SLAP_OP_SEARCH]++;
1018 case LDAP_REQ_ABANDON:
1019 num_ops_completed_[SLAP_OP_ABANDON]++;
1021 case LDAP_REQ_EXTENDED:
1022 num_ops_completed_[SLAP_OP_EXTENDED]++;
1025 #endif /* SLAPD_MONITOR */
1026 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1028 if ( arg->co_op->o_cancel == LDAP_CANCEL_REQ )
1029 arg->co_op->o_cancel = LDAP_TOO_LATE;
1031 while ( arg->co_op->o_cancel != LDAP_CANCEL_NONE &&
1032 arg->co_op->o_cancel != LDAP_CANCEL_DONE ) {
1033 ldap_pvt_thread_yield();
1036 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1038 conn->c_n_ops_executing--;
1039 conn->c_n_ops_completed++;
1041 LDAP_STAILQ_REMOVE( &conn->c_ops, arg->co_op, slap_op, o_next);
1042 LDAP_STAILQ_NEXT(arg->co_op, o_next) = NULL;
1043 #ifdef LDAP_CLIENT_UPDATE
1044 if ( !( arg->co_op->o_clientupdate_type & SLAP_LCUP_PERSIST ) )
1045 #endif /* LDAP_CLIENT_UPDATE */
1047 slap_op_free( arg->co_op );
1050 arg->co_conn = NULL;
1051 free( (char *) arg );
1056 case LDAP_REQ_UNBIND:
1057 /* c_mutex is locked */
1058 connection_closing( conn );
1062 conn->c_sasl_bind_in_progress =
1063 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1065 if( conn->c_conn_state == SLAP_C_BINDING) {
1066 conn->c_conn_state = SLAP_C_ACTIVE;
1070 connection_resched( conn );
1072 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1077 int connection_read(ber_socket_t s)
1082 assert( connections != NULL );
1084 ldap_pvt_thread_mutex_lock( &connections_mutex );
1086 /* get (locked) connection */
1087 c = connection_get( s );
1091 LDAP_LOG( CONNECTION, INFO,
1092 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1094 Debug( LDAP_DEBUG_ANY,
1095 "connection_read(%ld): no connection!\n",
1100 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1106 if( c->c_conn_state == SLAP_C_CLOSING ) {
1108 LDAP_LOG( CONNECTION, INFO,
1109 "connection_read: conn %lu connection closing, ignoring input\n",
1110 c->c_connid, 0, 0 );
1112 Debug( LDAP_DEBUG_TRACE,
1113 "connection_read(%d): closing, ignoring input for id=%lu\n",
1114 s, c->c_connid, 0 );
1116 connection_return( c );
1117 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1122 LDAP_LOG( CONNECTION, DETAIL1,
1123 "connection_read: conn %lu checking for input.\n",
1124 c->c_connid, 0, 0 );
1126 Debug( LDAP_DEBUG_TRACE,
1127 "connection_read(%d): checking for input on id=%lu\n",
1128 s, c->c_connid, 0 );
1132 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1133 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1135 #if 0 /* required by next #if 0 */
1141 LDAP_LOG( CONNECTION, ERR,
1142 "connection_read: conn %lu TLS accept error, error %d\n",
1143 c->c_connid, rc, 0 );
1145 Debug( LDAP_DEBUG_TRACE,
1146 "connection_read(%d): TLS accept error "
1147 "error=%d id=%lu, closing\n",
1148 s, rc, c->c_connid );
1150 c->c_needs_tls_accept = 0;
1151 /* connections_mutex and c_mutex are locked */
1152 connection_closing( c );
1155 /* Drain input before close, to allow SSL error codes
1156 * to propagate to client. */
1162 rc = select(s+1, &rfd, NULL, NULL, &tv);
1164 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1168 connection_close( c );
1170 } else if ( rc == 0 ) {
1172 struct berval authid = { 0, NULL };
1174 c->c_needs_tls_accept = 0;
1176 /* we need to let SASL know */
1177 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1179 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1180 if( c->c_tls_ssf > c->c_ssf ) {
1181 c->c_ssf = c->c_tls_ssf;
1184 rc = dnX509peerNormalize( ssl, &authid );
1185 if ( rc != LDAP_SUCCESS ) {
1187 LDAP_LOG( CONNECTION, INFO,
1188 "connection_read: conn %lu unable to get TLS client DN, "
1189 "error %d\n", c->c_connid, rc, 0 );
1191 Debug( LDAP_DEBUG_TRACE,
1192 "connection_read(%d): unable to get TLS client DN "
1193 "error=%d id=%lu\n",
1194 s, rc, c->c_connid );
1197 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1198 if ( authid.bv_val ) free( authid.bv_val );
1201 /* if success and data is ready, fall thru to data input loop */
1203 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1205 connection_return( c );
1206 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1212 #ifdef HAVE_CYRUS_SASL
1213 if ( c->c_sasl_layers ) {
1214 c->c_sasl_layers = 0;
1216 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1218 if( rc != LDAP_SUCCESS ) {
1220 LDAP_LOG( CONNECTION, ERR,
1221 "connection_read: conn %lu SASL install error %d, closing\n",
1222 c->c_connid, rc, 0 );
1224 Debug( LDAP_DEBUG_TRACE,
1225 "connection_read(%d): SASL install error "
1226 "error=%d id=%lu, closing\n",
1227 s, rc, c->c_connid );
1229 /* connections_mutex and c_mutex are locked */
1230 connection_closing( c );
1231 connection_close( c );
1232 connection_return( c );
1233 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1239 #define CONNECTION_INPUT_LOOP 1
1240 /* #define DATA_READY_LOOP 1 */
1244 /* How do we do this without getting into a busy loop ? */
1245 rc = connection_input( c );
1247 #ifdef DATA_READY_LOOP
1248 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) );
1249 #elif CONNECTION_INPUT_LOOP
1257 LDAP_LOG( CONNECTION, ERR,
1258 "connection_read: conn %lu input error %d, closing.\n",
1259 c->c_connid, rc, 0 );
1261 Debug( LDAP_DEBUG_TRACE,
1262 "connection_read(%d): input error=%d id=%lu, closing.\n",
1263 s, rc, c->c_connid );
1265 /* connections_mutex and c_mutex are locked */
1266 connection_closing( c );
1267 connection_close( c );
1268 connection_return( c );
1269 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1273 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1274 slapd_set_read( s, 1 );
1277 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1278 slapd_set_write( s, 1 );
1281 connection_return( c );
1282 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1296 #ifdef LDAP_CONNECTIONLESS
1301 if ( conn->c_currentber == NULL &&
1302 ( conn->c_currentber = ber_alloc()) == NULL )
1305 LDAP_LOG( CONNECTION, ERR,
1306 "connection_input: conn %lu ber_alloc failed.\n",
1307 conn->c_connid, 0, 0 );
1309 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1316 #ifdef LDAP_CONNECTIONLESS
1317 if ( conn->c_is_udp ) {
1318 char peername[sizeof("IP=255.255.255.255:65336")];
1319 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1320 sizeof(struct sockaddr));
1321 if (len != sizeof(struct sockaddr))
1323 sprintf( peername, "IP=%s:%d",
1324 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1325 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1326 Statslog( LDAP_DEBUG_STATS,
1327 "conn=%lu UDP request from %s (%s) accepted.\n",
1328 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1331 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1332 if ( tag != LDAP_TAG_MESSAGE ) {
1336 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1339 LDAP_LOG( CONNECTION, ERR,
1340 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1341 conn->c_connid, err, sock_errstr(err) );
1343 Debug( LDAP_DEBUG_TRACE,
1344 "ber_get_next on fd %d failed errno=%d (%s)\n",
1345 sd, err, sock_errstr(err) );
1347 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1348 /* log, close and send error */
1349 ber_free( conn->c_currentber, 1 );
1350 conn->c_currentber = NULL;
1357 ber = conn->c_currentber;
1358 conn->c_currentber = NULL;
1360 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1361 /* log, close and send error */
1363 LDAP_LOG( CONNECTION, ERR,
1364 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1365 conn->c_connid, tag, 0 );
1367 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1374 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1375 /* log, close and send error */
1377 LDAP_LOG( CONNECTION, ERR,
1378 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1379 conn->c_connid, tag, 0 );
1381 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1389 #ifdef LDAP_CONNECTIONLESS
1390 if( conn->c_is_udp ) {
1391 if( tag == LBER_OCTETSTRING ) {
1392 ber_get_stringa( ber, &cdn );
1393 tag = ber_peek_tag(ber, &len);
1395 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1397 LDAP_LOG( CONNECTION, ERR,
1398 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1399 conn->c_connid, tag, 0 );
1401 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1409 if(tag == LDAP_REQ_BIND) {
1410 /* immediately abandon all exiting operations upon BIND */
1411 connection_abandon( conn );
1414 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1417 op->vrFilter = NULL;
1418 op->o_pagedresults_state = conn->c_pagedresults_state;
1419 #ifdef LDAP_CONNECTIONLESS
1420 op->o_peeraddr = peeraddr;
1422 ber_str2bv( cdn, 0, 1, &op->o_dn );
1423 op->o_protocol = LDAP_VERSION2;
1425 if (conn->c_is_udp) {
1428 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1429 if (op->o_res_ber == NULL)
1432 rc = ber_write(op->o_res_ber, (char *)&op->o_peeraddr, sizeof(struct sockaddr), 0);
1433 if (rc != sizeof(struct sockaddr)) {
1435 LDAP_LOG( CONNECTION, INFO,
1436 "connection_input: conn %lu ber_write failed\n",
1437 conn->c_connid, 0, 0 );
1439 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1444 if (conn->c_protocol == LDAP_VERSION2) {
1445 rc = ber_printf(op->o_res_ber, "{i{" /*}}*/, op->o_msgid);
1448 LDAP_LOG( CONNECTION, INFO,
1449 "connection_input: conn %lu put outer sequence failed\n",
1450 conn->c_connid, 0, 0 );
1452 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1458 #endif /* LDAP_CONNECTIONLESS */
1460 if ( conn->c_conn_state == SLAP_C_BINDING
1461 || conn->c_conn_state == SLAP_C_CLOSING )
1464 LDAP_LOG( CONNECTION, INFO,
1465 "connection_input: conn %lu deferring operation\n",
1466 conn->c_connid, 0, 0 );
1468 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1470 conn->c_n_ops_pending++;
1471 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1474 conn->c_n_ops_executing++;
1475 connection_op_activate( conn, op );
1479 if ( conn->c_struct_state != SLAP_C_USED ) {
1480 /* connection must have got closed underneath us */
1484 assert( conn->c_struct_state == SLAP_C_USED );
1490 connection_resched( Connection *conn )
1494 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1497 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1499 /* us trylock to avoid possible deadlock */
1500 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1504 LDAP_LOG( CONNECTION, DETAIL1,
1505 "connection_resched: conn %lu reaquiring locks.\n",
1506 conn->c_connid, 0, 0 );
1508 Debug( LDAP_DEBUG_TRACE,
1509 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1510 conn->c_connid, sd, 0 );
1513 * reaquire locks in the right order...
1514 * this may allow another thread to close this connection,
1515 * so recheck state below.
1517 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1518 ldap_pvt_thread_mutex_lock( &connections_mutex );
1519 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1522 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1524 LDAP_LOG( CONNECTION, INFO,
1525 "connection_resched: conn %lu closed by other thread.\n",
1526 conn->c_connid, 0, 0 );
1528 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1529 "closed by other thread conn=%lu sd=%d\n",
1530 conn->c_connid, sd, 0 );
1534 LDAP_LOG( CONNECTION, DETAIL1,
1535 "connection_resched: conn %lu attempting closing.\n",
1536 conn->c_connid, 0, 0 );
1538 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1539 "attempting closing conn=%lu sd=%d\n",
1540 conn->c_connid, sd, 0 );
1542 connection_close( conn );
1545 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1549 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1550 /* other states need different handling */
1554 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1555 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1556 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1557 /* pending operations should not be marked for abandonment */
1558 assert(!op->o_abandon);
1560 conn->c_n_ops_pending--;
1561 conn->c_n_ops_executing++;
1563 connection_op_activate( conn, op );
1565 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1572 static int connection_op_activate( Connection *conn, Operation *op )
1576 ber_tag_t tag = op->o_tag;
1578 if(tag == LDAP_REQ_BIND) {
1579 conn->c_conn_state = SLAP_C_BINDING;
1582 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1583 arg->co_conn = conn;
1586 if (!arg->co_op->o_dn.bv_len) {
1587 arg->co_op->o_authz = conn->c_authz;
1588 arg->co_op->o_dn.bv_val = ch_strdup( conn->c_dn.bv_val ?
1589 conn->c_dn.bv_val : "" );
1590 arg->co_op->o_ndn.bv_val = ch_strdup( conn->c_ndn.bv_val ?
1591 conn->c_ndn.bv_val : "" );
1593 arg->co_op->o_authtype = conn->c_authtype;
1594 ber_dupbv( &arg->co_op->o_authmech, &conn->c_authmech );
1596 if (!arg->co_op->o_protocol) {
1597 arg->co_op->o_protocol = conn->c_protocol
1598 ? conn->c_protocol : LDAP_VERSION3;
1600 arg->co_op->o_connid = conn->c_connid;
1602 LDAP_STAILQ_INSERT_TAIL( &conn->c_ops, arg->co_op, o_next );
1604 status = ldap_pvt_thread_pool_submit( &connection_pool,
1605 connection_operation, (void *) arg );
1607 if ( status != 0 ) {
1609 LDAP_LOG( CONNECTION, ERR,
1610 "connection_op_activate: conn %lu thread pool submit failed.\n",
1611 conn->c_connid, 0, 0 );
1613 Debug( LDAP_DEBUG_ANY,
1614 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1616 /* should move op to pending list */
1622 int connection_write(ber_socket_t s)
1626 assert( connections != NULL );
1628 ldap_pvt_thread_mutex_lock( &connections_mutex );
1630 c = connection_get( s );
1632 slapd_clr_write( s, 0);
1636 LDAP_LOG( CONNECTION, ERR,
1637 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1639 Debug( LDAP_DEBUG_ANY,
1640 "connection_write(%ld): no connection!\n",
1644 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1651 LDAP_LOG( CONNECTION, DETAIL1,
1652 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1654 Debug( LDAP_DEBUG_TRACE,
1655 "connection_write(%d): waking output for id=%lu\n",
1656 s, c->c_connid, 0 );
1658 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1660 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1661 slapd_set_read( s, 1 );
1662 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1663 slapd_set_write( s, 1 );
1664 connection_return( c );
1665 ldap_pvt_thread_mutex_unlock( &connections_mutex );