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_mutex_destroy( &connections[i].c_sasl_bindmutex );
143 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
150 ldap_pvt_thread_mutex_destroy( &connections_mutex );
155 * shutdown all connections
157 int connections_shutdown(void)
161 ldap_pvt_thread_mutex_lock( &connections_mutex );
163 for ( i = 0; i < dtblsize; i++ ) {
164 if( connections[i].c_struct_state != SLAP_C_USED ) {
168 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
170 /* connections_mutex and c_mutex are locked */
171 connection_closing( &connections[i] );
172 connection_close( &connections[i] );
174 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
177 ldap_pvt_thread_mutex_unlock( &connections_mutex );
183 * Timeout idle connections.
185 int connections_timeout_idle(time_t now)
191 for( c = connection_first( &connindex );
193 c = connection_next( c, &connindex ) )
195 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
197 connection_closing( c );
198 connection_close( c );
202 connection_done( c );
207 static Connection* connection_get( ber_socket_t s )
209 /* connections_mutex should be locked by caller */
214 LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
216 Debug( LDAP_DEBUG_ARGS,
217 "connection_get(%ld)\n",
221 assert( connections != NULL );
223 if(s == AC_SOCKET_INVALID) {
230 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
237 for(i=0; i<dtblsize; i++) {
238 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
239 assert( connections[i].c_conn_state == SLAP_C_INVALID );
240 assert( connections[i].c_sb == 0 );
244 ber_sockbuf_ctrl( connections[i].c_sb,
245 LBER_SB_OPT_GET_FD, &sd );
247 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
248 assert( connections[i].c_conn_state == SLAP_C_INVALID );
249 assert( sd == AC_SOCKET_INVALID );
253 /* state can actually change from used -> unused by resched,
254 * so don't assert details here.
268 ldap_pvt_thread_mutex_lock( &c->c_mutex );
270 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
271 if( c->c_struct_state != SLAP_C_USED ) {
272 /* connection must have been closed due to resched */
274 assert( c->c_conn_state == SLAP_C_INVALID );
275 assert( sd == AC_SOCKET_INVALID );
278 LDAP_LOG( CONNECTION, ARGS,
279 "connection_get: connection %d not used\n", s, 0, 0 );
281 Debug( LDAP_DEBUG_TRACE,
282 "connection_get(%d): connection not used\n",
286 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
291 LDAP_LOG( CONNECTION, RESULTS,
292 "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
294 Debug( LDAP_DEBUG_TRACE,
295 "connection_get(%d): got connid=%lu\n",
301 assert( c->c_struct_state == SLAP_C_USED );
302 assert( c->c_conn_state != SLAP_C_INVALID );
303 assert( sd != AC_SOCKET_INVALID );
306 c->c_activitytime = slap_get_time();
308 if( global_idletimeout > 0 ) {
309 c->c_activitytime = slap_get_time();
317 static void connection_return( Connection *c )
319 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
322 long connection_init(
326 const char* peername,
327 const char* sockname,
335 assert( connections != NULL );
337 assert( dnsname != NULL );
338 assert( peername != NULL );
339 assert( sockname != NULL );
342 assert( tls_udp_option != 1 );
345 if( s == AC_SOCKET_INVALID ) {
347 LDAP_LOG( CONNECTION, INFO,
348 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
350 Debug( LDAP_DEBUG_ANY,
351 "connection_init(%ld): invalid.\n",
359 assert( s < dtblsize );
362 ldap_pvt_thread_mutex_lock( &connections_mutex );
373 for( i=0; i < dtblsize; i++) {
376 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
377 assert( connections[i].c_sb == 0 );
382 sd = AC_SOCKET_INVALID;
383 if (connections[i].c_sb != NULL)
384 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
386 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
387 assert( sd == AC_SOCKET_INVALID );
392 assert( connections[i].c_struct_state == SLAP_C_USED );
393 assert( connections[i].c_conn_state != SLAP_C_INVALID );
394 assert( sd != AC_SOCKET_INVALID );
399 LDAP_LOG( CONNECTION, INFO,
400 "connection_init: skt %d connection table full (%d/%d)\n",
403 Debug( LDAP_DEBUG_ANY,
404 "connection_init(%d): connection table full (%d/%d)\n",
407 ldap_pvt_thread_mutex_unlock( &connections_mutex );
415 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
416 c->c_authmech.bv_val = NULL;
417 c->c_authmech.bv_len = 0;
418 c->c_dn.bv_val = NULL;
420 c->c_ndn.bv_val = NULL;
424 c->c_listener_url.bv_val = NULL;
425 c->c_listener_url.bv_len = 0;
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;
430 c->c_sock_name.bv_val = NULL;
431 c->c_sock_name.bv_len = 0;
433 LDAP_STAILQ_INIT(&c->c_ops);
434 LDAP_STAILQ_INIT(&c->c_pending_ops);
436 c->c_sasl_bind_mech.bv_val = NULL;
437 c->c_sasl_bind_mech.bv_len = 0;
438 c->c_sasl_context = 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_mutex_init( &c->c_sasl_bindmutex );
455 ldap_pvt_thread_cond_init( &c->c_write_cv );
457 c->c_struct_state = SLAP_C_UNUSED;
460 ldap_pvt_thread_mutex_lock( &c->c_mutex );
462 assert( c->c_struct_state == SLAP_C_UNUSED );
463 assert( c->c_authmech.bv_val == NULL );
464 assert( c->c_dn.bv_val == NULL );
465 assert( c->c_ndn.bv_val == NULL );
466 assert( c->c_groups == NULL );
467 assert( c->c_listener_url.bv_val == NULL );
468 assert( c->c_peer_domain.bv_val == NULL );
469 assert( c->c_peer_name.bv_val == NULL );
470 assert( c->c_sock_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 ber_str2bv( url, 0, 1, &c->c_listener_url );
480 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
481 ber_str2bv( peername, 0, 1, &c->c_peer_name );
482 ber_str2bv( sockname, 0, 1, &c->c_sock_name );
484 c->c_n_ops_received = 0;
485 c->c_n_ops_executing = 0;
486 c->c_n_ops_pending = 0;
487 c->c_n_ops_completed = 0;
493 /* set to zero until bind, implies LDAP_VERSION3 */
497 c->c_activitytime = c->c_starttime = slap_get_time();
499 if( global_idletimeout > 0 ) {
500 c->c_activitytime = c->c_starttime = slap_get_time();
504 #ifdef LDAP_CONNECTIONLESS
506 if (tls_udp_option == 2)
510 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
511 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
513 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
514 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
515 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
516 LBER_SBIOD_LEVEL_PROVIDER, NULL );
521 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
522 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
524 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
525 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
529 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
530 INT_MAX, (void*)"ldap_" );
533 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
534 c /* non-NULL */ ) < 0 )
537 LDAP_LOG( CONNECTION, INFO,
538 "connection_init: conn %lu set nonblocking failed\n",
541 Debug( LDAP_DEBUG_ANY,
542 "connection_init(%d, %s): set nonblocking failed\n",
543 s, c->c_peer_name.bv_val, 0 );
547 id = c->c_connid = conn_nextid++;
549 c->c_conn_state = SLAP_C_INACTIVE;
550 c->c_struct_state = SLAP_C_USED;
552 c->c_ssf = c->c_transport_ssf = ssf;
556 if ( tls_udp_option == 1 ) {
558 c->c_needs_tls_accept = 1;
561 c->c_needs_tls_accept = 0;
566 slap_sasl_external( c, ssf, authid );
568 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
569 ldap_pvt_thread_mutex_unlock( &connections_mutex );
571 backend_connection_init(c);
576 void connection2anonymous( Connection *c )
578 assert( connections != NULL );
582 ber_len_t max = sockbuf_max_incoming;
583 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
586 if(c->c_authmech.bv_val != NULL ) {
587 free(c->c_authmech.bv_val);
588 c->c_authmech.bv_val = NULL;
590 c->c_authmech.bv_len = 0;
592 if(c->c_dn.bv_val != NULL) {
593 free(c->c_dn.bv_val);
594 c->c_dn.bv_val = NULL;
597 if(c->c_ndn.bv_val != NULL) {
598 free(c->c_ndn.bv_val);
599 c->c_ndn.bv_val = NULL;
603 c->c_authz_backend = NULL;
606 GroupAssertion *g, *n;
607 for (g = c->c_groups; g; g=n) {
616 connection_destroy( Connection *c )
618 /* note: connections_mutex should be locked by caller */
620 unsigned long connid;
622 assert( connections != NULL );
624 assert( c->c_struct_state != SLAP_C_UNUSED );
625 assert( c->c_conn_state != SLAP_C_INVALID );
626 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
628 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
629 connid = c->c_connid;
631 backend_connection_destroy(c);
636 c->c_activitytime = c->c_starttime = 0;
638 connection2anonymous( c );
640 if(c->c_listener_url.bv_val != NULL) {
641 free(c->c_listener_url.bv_val);
642 c->c_listener_url.bv_val = NULL;
644 c->c_listener_url.bv_len = 0;
646 if(c->c_peer_domain.bv_val != NULL) {
647 free(c->c_peer_domain.bv_val);
648 c->c_peer_domain.bv_val = NULL;
650 c->c_peer_domain.bv_len = 0;
651 if(c->c_peer_name.bv_val != NULL) {
654 * If peer was a domain socket, unlink. Mind you,
655 * they may be un-named. Should we leave this to
658 if (strncmp(c->c_peer_name.bv_val, "PATH=",
659 sizeof("PATH=") - 1) == 0) {
660 char *path = c->c_peer_name.bv_val
661 + sizeof("PATH=") - 1;
662 if (path[0] != '\0') {
666 #endif /* LDAP_PF_LOCAL */
668 free(c->c_peer_name.bv_val);
669 c->c_peer_name.bv_val = NULL;
671 c->c_peer_name.bv_len = 0;
672 if(c->c_sock_name.bv_val != NULL) {
673 free(c->c_sock_name.bv_val);
674 c->c_sock_name.bv_val = NULL;
676 c->c_sock_name.bv_len = 0;
678 c->c_sasl_bind_in_progress = 0;
679 if(c->c_sasl_bind_mech.bv_val != NULL) {
680 free(c->c_sasl_bind_mech.bv_val);
681 c->c_sasl_bind_mech.bv_val = NULL;
683 c->c_sasl_bind_mech.bv_len = 0;
685 slap_sasl_close( c );
687 if ( c->c_currentber != NULL ) {
688 ber_free( c->c_currentber, 1 );
689 c->c_currentber = NULL;
692 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
693 if ( sd != AC_SOCKET_INVALID ) {
694 slapd_remove( sd, 0 );
696 Statslog( LDAP_DEBUG_STATS,
697 "conn=%lu fd=%d closed\n",
698 connid, sd, 0, 0, 0 );
701 ber_sockbuf_free( c->c_sb );
703 c->c_sb = ber_sockbuf_alloc( );
706 ber_len_t max = sockbuf_max_incoming;
707 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
710 c->c_conn_state = SLAP_C_INVALID;
711 c->c_struct_state = SLAP_C_UNUSED;
714 int connection_state_closing( Connection *c )
716 /* c_mutex must be locked by caller */
720 assert( c->c_struct_state == SLAP_C_USED );
722 state = c->c_conn_state;
724 assert( state != SLAP_C_INVALID );
726 return state == SLAP_C_CLOSING;
729 static void connection_abandon( Connection *c )
731 /* c_mutex must be locked by caller */
735 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
739 /* remove pending operations */
740 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
741 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
742 LDAP_STAILQ_NEXT(o, o_next) = NULL;
747 void connection_closing( Connection *c )
749 assert( connections != NULL );
751 assert( c->c_struct_state == SLAP_C_USED );
752 assert( c->c_conn_state != SLAP_C_INVALID );
754 /* c_mutex must be locked by caller */
756 if( c->c_conn_state != SLAP_C_CLOSING ) {
759 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
761 LDAP_LOG( CONNECTION, DETAIL1,
762 "connection_closing: conn %lu readying socket %d for close.\n",
763 c->c_connid, sd, 0 );
765 Debug( LDAP_DEBUG_TRACE,
766 "connection_closing: readying conn=%lu sd=%d for close\n",
767 c->c_connid, sd, 0 );
769 /* update state to closing */
770 c->c_conn_state = SLAP_C_CLOSING;
772 /* don't listen on this port anymore */
773 slapd_clr_read( sd, 1 );
775 /* abandon active operations */
776 connection_abandon( c );
778 /* wake write blocked operations */
779 slapd_clr_write( sd, 1 );
780 ldap_pvt_thread_cond_signal( &c->c_write_cv );
784 static void connection_close( Connection *c )
788 assert( connections != NULL );
790 assert( c->c_struct_state == SLAP_C_USED );
791 assert( c->c_conn_state == SLAP_C_CLOSING );
793 /* note: connections_mutex and c_mutex should be locked by caller */
795 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
796 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
798 LDAP_LOG( CONNECTION, DETAIL1,
799 "connection_close: conn %lu deferring sd %d\n",
800 c->c_connid, sd, 0 );
802 Debug( LDAP_DEBUG_TRACE,
803 "connection_close: deferring conn=%lu sd=%d\n",
804 c->c_connid, sd, 0 );
810 LDAP_LOG( CONNECTION, RESULTS,
811 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
813 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
814 c->c_connid, sd, 0 );
816 connection_destroy( c );
819 unsigned long connections_nextid(void)
822 assert( connections != NULL );
824 ldap_pvt_thread_mutex_lock( &connections_mutex );
828 ldap_pvt_thread_mutex_unlock( &connections_mutex );
833 Connection* connection_first( ber_socket_t *index )
835 assert( connections != NULL );
836 assert( index != NULL );
838 ldap_pvt_thread_mutex_lock( &connections_mutex );
842 return connection_next(NULL, index);
845 Connection* connection_next( Connection *c, ber_socket_t *index )
847 assert( connections != NULL );
848 assert( index != NULL );
849 assert( *index <= dtblsize );
852 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
857 for(; *index < dtblsize; (*index)++) {
858 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
859 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
867 if( connections[*index].c_struct_state == SLAP_C_USED ) {
868 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
869 c = &connections[(*index)++];
873 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
874 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
878 ldap_pvt_thread_mutex_lock( &c->c_mutex );
884 void connection_done( Connection *c )
886 assert( connections != NULL );
889 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
892 ldap_pvt_thread_mutex_unlock( &connections_mutex );
896 * connection_activity - handle the request operation op on connection
897 * conn. This routine figures out what kind of operation it is and
898 * calls the appropriate stub to handle it.
902 #define INCR_OP(var,index) \
904 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
906 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
908 #else /* !SLAPD_MONITOR */
909 #define INCR_OP(var,index)
910 #endif /* !SLAPD_MONITOR */
913 connection_operation( void *ctx, void *arg_v )
916 struct co_arg *arg = arg_v;
917 ber_tag_t tag = arg->co_op->o_tag;
919 ber_tag_t oldtag = tag;
920 #endif /* SLAPD_MONITOR */
921 Connection *conn = arg->co_conn;
923 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
925 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
927 arg->co_op->o_threadctx = ctx;
929 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
931 LDAP_LOG( CONNECTION, ERR,
932 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
933 conn->c_connid, (long)tag, 0 );
935 Debug( LDAP_DEBUG_ANY, "connection_operation: "
936 "error: SASL bind in progress (tag=%ld).\n",
939 send_ldap_result( conn, arg->co_op,
940 rc = LDAP_OPERATIONS_ERROR,
941 NULL, "SASL bind in progress", NULL, NULL );
942 goto operations_error;
947 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
948 rc = do_bind( conn, arg->co_op );
951 case LDAP_REQ_UNBIND:
952 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
953 rc = do_unbind( conn, arg->co_op );
957 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
958 rc = do_add( conn, arg->co_op );
961 case LDAP_REQ_DELETE:
962 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
963 rc = do_delete( conn, arg->co_op );
966 case LDAP_REQ_MODRDN:
967 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
968 rc = do_modrdn( conn, arg->co_op );
971 case LDAP_REQ_MODIFY:
972 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
973 rc = do_modify( conn, arg->co_op );
976 case LDAP_REQ_COMPARE:
977 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
978 rc = do_compare( conn, arg->co_op );
981 case LDAP_REQ_SEARCH:
982 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
983 rc = do_search( conn, arg->co_op );
986 case LDAP_REQ_ABANDON:
987 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
988 rc = do_abandon( conn, arg->co_op );
991 case LDAP_REQ_EXTENDED:
992 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
993 rc = do_extended( conn, arg->co_op );
998 LDAP_LOG( CONNECTION, INFO,
999 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
1000 conn->c_connid, tag, 0 );
1002 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
1005 arg->co_op->o_tag = LBER_ERROR;
1006 send_ldap_disconnect( conn, arg->co_op,
1007 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
1012 #ifdef SLAPD_MONITOR
1014 #endif /* SLAPD_MONITOR */
1015 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1018 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1019 num_ops_completed++;
1020 #ifdef SLAPD_MONITOR
1023 num_ops_completed_[SLAP_OP_BIND]++;
1025 case LDAP_REQ_UNBIND:
1026 num_ops_completed_[SLAP_OP_UNBIND]++;
1029 num_ops_completed_[SLAP_OP_ADD]++;
1031 case LDAP_REQ_DELETE:
1032 num_ops_completed_[SLAP_OP_DELETE]++;
1034 case LDAP_REQ_MODRDN:
1035 num_ops_completed_[SLAP_OP_MODRDN]++;
1037 case LDAP_REQ_MODIFY:
1038 num_ops_completed_[SLAP_OP_MODIFY]++;
1040 case LDAP_REQ_COMPARE:
1041 num_ops_completed_[SLAP_OP_COMPARE]++;
1043 case LDAP_REQ_SEARCH:
1044 num_ops_completed_[SLAP_OP_SEARCH]++;
1046 case LDAP_REQ_ABANDON:
1047 num_ops_completed_[SLAP_OP_ABANDON]++;
1049 case LDAP_REQ_EXTENDED:
1050 num_ops_completed_[SLAP_OP_EXTENDED]++;
1053 #endif /* SLAPD_MONITOR */
1054 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1056 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1058 conn->c_n_ops_executing--;
1059 conn->c_n_ops_completed++;
1061 LDAP_STAILQ_REMOVE( &conn->c_ops, arg->co_op, slap_op, o_next);
1062 LDAP_STAILQ_NEXT(arg->co_op, o_next) = NULL;
1063 slap_op_free( arg->co_op );
1065 arg->co_conn = NULL;
1066 free( (char *) arg );
1071 case LDAP_REQ_UNBIND:
1072 /* c_mutex is locked */
1073 connection_closing( conn );
1077 conn->c_sasl_bind_in_progress =
1078 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1080 if( conn->c_conn_state == SLAP_C_BINDING) {
1081 conn->c_conn_state = SLAP_C_ACTIVE;
1085 connection_resched( conn );
1087 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1092 int connection_read(ber_socket_t s)
1097 assert( connections != NULL );
1099 ldap_pvt_thread_mutex_lock( &connections_mutex );
1101 /* get (locked) connection */
1102 c = connection_get( s );
1106 LDAP_LOG( CONNECTION, INFO,
1107 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1109 Debug( LDAP_DEBUG_ANY,
1110 "connection_read(%ld): no connection!\n",
1115 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1121 if( c->c_conn_state == SLAP_C_CLOSING ) {
1123 LDAP_LOG( CONNECTION, INFO,
1124 "connection_read: conn %lu connection closing, ignoring input\n",
1125 c->c_connid, 0, 0 );
1127 Debug( LDAP_DEBUG_TRACE,
1128 "connection_read(%d): closing, ignoring input for id=%lu\n",
1129 s, c->c_connid, 0 );
1131 connection_return( c );
1132 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1137 LDAP_LOG( CONNECTION, DETAIL1,
1138 "connection_read: conn %lu checking for input.\n",
1139 c->c_connid, 0, 0 );
1141 Debug( LDAP_DEBUG_TRACE,
1142 "connection_read(%d): checking for input on id=%lu\n",
1143 s, c->c_connid, 0 );
1147 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1148 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1150 #if 0 /* required by next #if 0 */
1156 LDAP_LOG( CONNECTION, ERR,
1157 "connection_read: conn %lu TLS accept error, error %d\n",
1158 c->c_connid, rc, 0 );
1160 Debug( LDAP_DEBUG_TRACE,
1161 "connection_read(%d): TLS accept error "
1162 "error=%d id=%lu, closing\n",
1163 s, rc, c->c_connid );
1165 c->c_needs_tls_accept = 0;
1166 /* connections_mutex and c_mutex are locked */
1167 connection_closing( c );
1170 /* Drain input before close, to allow SSL error codes
1171 * to propagate to client. */
1177 rc = select(s+1, &rfd, NULL, NULL, &tv);
1179 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1183 connection_close( c );
1185 } else if ( rc == 0 ) {
1187 struct berval authid = { 0, NULL };
1189 c->c_needs_tls_accept = 0;
1191 /* we need to let SASL know */
1192 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1194 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1195 if( c->c_tls_ssf > c->c_ssf ) {
1196 c->c_ssf = c->c_tls_ssf;
1199 rc = dnX509peerNormalize( ssl, &authid );
1200 if ( rc != LDAP_SUCCESS ) {
1202 LDAP_LOG( CONNECTION, INFO,
1203 "connection_read: conn %lu unable to get TLS client DN, "
1204 "error %d\n", c->c_connid, rc, 0 );
1206 Debug( LDAP_DEBUG_TRACE,
1207 "connection_read(%d): unable to get TLS client DN "
1208 "error=%d id=%lu\n",
1209 s, rc, c->c_connid );
1212 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1213 if ( authid.bv_val ) free( authid.bv_val );
1216 /* if success and data is ready, fall thru to data input loop */
1218 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1220 connection_return( c );
1221 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1227 #ifdef HAVE_CYRUS_SASL
1228 if ( c->c_sasl_layers ) {
1229 c->c_sasl_layers = 0;
1231 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1233 if( rc != LDAP_SUCCESS ) {
1235 LDAP_LOG( CONNECTION, ERR,
1236 "connection_read: conn %lu SASL install error %d, closing\n",
1237 c->c_connid, rc, 0 );
1239 Debug( LDAP_DEBUG_TRACE,
1240 "connection_read(%d): SASL install error "
1241 "error=%d id=%lu, closing\n",
1242 s, rc, c->c_connid );
1244 /* connections_mutex and c_mutex are locked */
1245 connection_closing( c );
1246 connection_close( c );
1247 connection_return( c );
1248 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1254 #define CONNECTION_INPUT_LOOP 1
1256 #ifdef DATA_READY_LOOP
1257 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1258 #elif CONNECTION_INPUT_LOOP
1262 /* How do we do this without getting into a busy loop ? */
1263 rc = connection_input( c );
1268 LDAP_LOG( CONNECTION, ERR,
1269 "connection_read: conn %lu input error %d, closing.\n",
1270 c->c_connid, rc, 0 );
1272 Debug( LDAP_DEBUG_TRACE,
1273 "connection_read(%d): input error=%d id=%lu, closing.\n",
1274 s, rc, c->c_connid );
1276 /* connections_mutex and c_mutex are locked */
1277 connection_closing( c );
1278 connection_close( c );
1279 connection_return( c );
1280 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1284 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1285 slapd_set_read( s, 1 );
1288 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1289 slapd_set_write( s, 1 );
1292 connection_return( c );
1293 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1307 #ifdef LDAP_CONNECTIONLESS
1312 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1315 LDAP_LOG( CONNECTION, ERR,
1316 "connection_input: conn %lu ber_alloc failed.\n",
1317 conn->c_connid, 0, 0 );
1319 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1326 #ifdef LDAP_CONNECTIONLESS
1329 char peername[sizeof("IP=255.255.255.255:65336")];
1330 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1331 sizeof(struct sockaddr));
1332 if (len != sizeof(struct sockaddr))
1334 sprintf( peername, "IP=%s:%d",
1335 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1336 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1337 Statslog( LDAP_DEBUG_STATS,
1338 "conn=%lu UDP request from %s (%s) accepted.\n",
1339 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1342 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1343 if ( tag != LDAP_TAG_MESSAGE ) {
1347 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1350 LDAP_LOG( CONNECTION, ERR,
1351 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1352 conn->c_connid, err, sock_errstr(err) );
1354 Debug( LDAP_DEBUG_TRACE,
1355 "ber_get_next on fd %d failed errno=%d (%s)\n",
1356 sd, err, sock_errstr(err) );
1358 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1359 /* log, close and send error */
1360 ber_free( conn->c_currentber, 1 );
1361 conn->c_currentber = NULL;
1368 ber = conn->c_currentber;
1369 conn->c_currentber = NULL;
1371 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1372 /* log, close and send error */
1374 LDAP_LOG( CONNECTION, ERR,
1375 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1376 conn->c_connid, tag, 0 );
1378 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1385 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1386 /* log, close and send error */
1388 LDAP_LOG( CONNECTION, ERR,
1389 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1390 conn->c_connid, tag, 0 );
1392 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1400 #ifdef LDAP_CONNECTIONLESS
1401 if (conn->c_is_udp) {
1402 if (tag == LBER_OCTETSTRING) {
1403 ber_get_stringa( ber, &cdn );
1404 tag = ber_peek_tag(ber, &len);
1406 if (tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH) {
1408 LDAP_LOG( CONNECTION, ERR,
1409 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1410 conn->c_connid, tag, 0 );
1412 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1420 if(tag == LDAP_REQ_BIND) {
1421 /* immediately abandon all exiting operations upon BIND */
1422 connection_abandon( conn );
1425 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1427 op->vrFilter = NULL;
1429 op->o_pagedresults_state = conn->c_pagedresults_state;
1431 #ifdef LDAP_CONNECTIONLESS
1432 op->o_peeraddr = peeraddr;
1434 ber_str2bv( cdn, 0, 1, &op->o_dn );
1435 op->o_protocol = LDAP_VERSION2;
1438 if ( conn->c_conn_state == SLAP_C_BINDING
1439 || conn->c_conn_state == SLAP_C_CLOSING )
1442 LDAP_LOG( CONNECTION, INFO,
1443 "connection_input: conn %lu deferring operation\n",
1444 conn->c_connid, 0, 0 );
1446 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1448 conn->c_n_ops_pending++;
1449 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1452 conn->c_n_ops_executing++;
1453 connection_op_activate( conn, op );
1457 if ( conn->c_struct_state != SLAP_C_USED ) {
1458 /* connection must have got closed underneath us */
1462 assert( conn->c_struct_state == SLAP_C_USED );
1468 connection_resched( Connection *conn )
1472 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1475 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1477 /* us trylock to avoid possible deadlock */
1478 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1482 LDAP_LOG( CONNECTION, DETAIL1,
1483 "connection_resched: conn %lu reaquiring locks.\n",
1484 conn->c_connid, 0, 0 );
1486 Debug( LDAP_DEBUG_TRACE,
1487 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1488 conn->c_connid, sd, 0 );
1491 * reaquire locks in the right order...
1492 * this may allow another thread to close this connection,
1493 * so recheck state below.
1495 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1496 ldap_pvt_thread_mutex_lock( &connections_mutex );
1497 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1500 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1502 LDAP_LOG( CONNECTION, INFO,
1503 "connection_resched: conn %lu closed by other thread.\n",
1504 conn->c_connid, 0, 0 );
1506 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1507 "closed by other thread conn=%lu sd=%d\n",
1508 conn->c_connid, sd, 0 );
1512 LDAP_LOG( CONNECTION, DETAIL1,
1513 "connection_resched: conn %lu attempting closing.\n",
1514 conn->c_connid, 0, 0 );
1516 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1517 "attempting closing conn=%lu sd=%d\n",
1518 conn->c_connid, sd, 0 );
1520 connection_close( conn );
1523 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1527 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1528 /* other states need different handling */
1532 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1533 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1534 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1535 /* pending operations should not be marked for abandonment */
1536 assert(!op->o_abandon);
1538 conn->c_n_ops_pending--;
1539 conn->c_n_ops_executing++;
1541 connection_op_activate( conn, op );
1543 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1550 static int connection_op_activate( Connection *conn, Operation *op )
1554 ber_tag_t tag = op->o_tag;
1556 if(tag == LDAP_REQ_BIND) {
1557 conn->c_conn_state = SLAP_C_BINDING;
1560 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1561 arg->co_conn = conn;
1564 if (!arg->co_op->o_dn.bv_len) {
1565 arg->co_op->o_authz = conn->c_authz;
1566 arg->co_op->o_dn.bv_val = ch_strdup( conn->c_dn.bv_val ?
1567 conn->c_dn.bv_val : "" );
1568 arg->co_op->o_ndn.bv_val = ch_strdup( conn->c_ndn.bv_val ?
1569 conn->c_ndn.bv_val : "" );
1571 arg->co_op->o_authtype = conn->c_authtype;
1572 ber_dupbv( &arg->co_op->o_authmech, &conn->c_authmech );
1574 if (!arg->co_op->o_protocol) {
1575 arg->co_op->o_protocol = conn->c_protocol
1576 ? conn->c_protocol : LDAP_VERSION3;
1578 arg->co_op->o_connid = conn->c_connid;
1580 LDAP_STAILQ_INSERT_TAIL( &conn->c_ops, arg->co_op, o_next );
1582 status = ldap_pvt_thread_pool_submit( &connection_pool,
1583 connection_operation, (void *) arg );
1585 if ( status != 0 ) {
1587 LDAP_LOG( CONNECTION, ERR,
1588 "connection_op_activate: conn %lu thread pool submit failed.\n",
1589 conn->c_connid, 0, 0 );
1591 Debug( LDAP_DEBUG_ANY,
1592 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1594 /* should move op to pending list */
1600 int connection_write(ber_socket_t s)
1604 assert( connections != NULL );
1606 ldap_pvt_thread_mutex_lock( &connections_mutex );
1608 c = connection_get( s );
1610 slapd_clr_write( s, 0);
1614 LDAP_LOG( CONNECTION, ERR,
1615 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1617 Debug( LDAP_DEBUG_ANY,
1618 "connection_write(%ld): no connection!\n",
1622 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1629 LDAP_LOG( CONNECTION, DETAIL1,
1630 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1632 Debug( LDAP_DEBUG_TRACE,
1633 "connection_write(%d): waking output for id=%lu\n",
1634 s, c->c_connid, 0 );
1636 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1638 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1639 slapd_set_read( s, 1 );
1640 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1641 slapd_set_write( s, 1 );
1642 connection_return( c );
1643 ldap_pvt_thread_mutex_unlock( &connections_mutex );