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/signal.h>
15 #include <ac/string.h>
17 #include <ac/unistd.h>
23 /* protected by connections_mutex */
24 static ldap_pvt_thread_mutex_t connections_mutex;
25 static Connection *connections = NULL;
26 static unsigned long conn_nextid = 0;
28 /* structure state (protected by connections_mutex) */
29 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
30 #define SLAP_C_UNUSED 0x01
31 #define SLAP_C_USED 0x02
33 /* connection state (protected by c_mutex ) */
34 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
35 #define SLAP_C_INACTIVE 0x01 /* zero threads */
36 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
37 #define SLAP_C_BINDING 0x03 /* binding */
38 #define SLAP_C_CLOSING 0x04 /* closing */
41 connection_state2str( int state )
44 case SLAP_C_INVALID: return "!";
45 case SLAP_C_INACTIVE: return "|";
46 case SLAP_C_ACTIVE: return "";
47 case SLAP_C_BINDING: return "B";
48 case SLAP_C_CLOSING: return "C";
54 static Connection* connection_get( ber_socket_t s );
56 static int connection_input( Connection *c );
57 static void connection_close( Connection *c );
59 static int connection_op_activate( Connection *conn, Operation *op );
60 static int connection_resched( Connection *conn );
61 static void connection_abandon( Connection *conn );
62 static void connection_destroy( Connection *c );
70 * Initialize connection management infrastructure.
72 int connections_init(void)
74 assert( connections == NULL );
76 if( connections != NULL) {
78 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
79 "connections_init: already initialized.\n" ));
81 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
87 /* should check return of every call */
88 ldap_pvt_thread_mutex_init( &connections_mutex );
90 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
92 if( connections == NULL ) {
94 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
95 "connections_init: allocation (%d * %ld) of connection array failed\n",
96 dtblsize, (long) sizeof(Connection) ));
98 Debug( LDAP_DEBUG_ANY,
99 "connections_init: allocation (%d*%ld) of connection array failed\n",
100 dtblsize, (long) sizeof(Connection), 0 );
105 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
106 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
109 * per entry initialization of the Connection array initialization
110 * will be done by connection_init()
117 * Destroy connection management infrastructure.
119 int connections_destroy(void)
123 /* should check return of every call */
125 if( connections == NULL) {
127 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
128 "connections_destroy: nothing to destroy.\n"));
130 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
136 for ( i = 0; i < dtblsize; i++ ) {
137 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
138 ber_sockbuf_free( connections[i].c_sb );
139 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
140 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
141 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
148 ldap_pvt_thread_mutex_destroy( &connections_mutex );
153 * shutdown all connections
155 int connections_shutdown(void)
159 ldap_pvt_thread_mutex_lock( &connections_mutex );
161 for ( i = 0; i < dtblsize; i++ ) {
162 if( connections[i].c_struct_state != SLAP_C_USED ) {
166 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
168 /* connections_mutex and c_mutex are locked */
169 connection_closing( &connections[i] );
170 connection_close( &connections[i] );
172 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
175 ldap_pvt_thread_mutex_unlock( &connections_mutex );
181 * Timeout idle connections.
183 int connections_timeout_idle(time_t now)
189 for( c = connection_first( &connindex );
191 c = connection_next( c, &connindex ) )
193 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
195 connection_closing( c );
196 connection_close( c );
200 connection_done( c );
205 static Connection* connection_get( ber_socket_t s )
207 /* connections_mutex should be locked by caller */
212 LDAP_LOG(( "connection", LDAP_LEVEL_ENTRY,
213 "connection_get: socket %ld\n", (long)s ));
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", LDAP_LEVEL_ARGS,
278 "connection_get: connection %d not used\n", s ));
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", LDAP_LEVEL_RESULTS,
291 "connection_get: get for %d got connid %lu\n", s, c->c_connid ));
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,
326 const char* sockname,
334 assert( connections != NULL );
336 assert( dnsname != NULL );
337 assert( peername != NULL );
338 assert( sockname != NULL );
341 assert( tls_udp_option != 1 );
344 if( s == AC_SOCKET_INVALID ) {
346 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
347 "connection_init: init of socket %ld invalid.\n", (long)s ));
349 Debug( LDAP_DEBUG_ANY,
350 "connection_init(%ld): invalid.\n",
358 assert( s < dtblsize );
361 ldap_pvt_thread_mutex_lock( &connections_mutex );
372 for( i=0; i < dtblsize; i++) {
375 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
376 assert( connections[i].c_sb == 0 );
381 sd = AC_SOCKET_INVALID;
382 if (connections[i].c_sb != NULL)
383 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
385 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
386 assert( sd == AC_SOCKET_INVALID );
391 assert( connections[i].c_struct_state == SLAP_C_USED );
392 assert( connections[i].c_conn_state != SLAP_C_INVALID );
393 assert( sd != AC_SOCKET_INVALID );
398 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
399 "connection_init: skt %d connection table full (%d/%d)\n",
402 Debug( LDAP_DEBUG_ANY,
403 "connection_init(%d): connection table full (%d/%d)\n",
406 ldap_pvt_thread_mutex_unlock( &connections_mutex );
414 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
415 c->c_authmech.bv_val = NULL;
416 c->c_authmech.bv_len = 0;
417 c->c_dn.bv_val = NULL;
419 c->c_ndn.bv_val = NULL;
421 c->c_cdn.bv_val = NULL;
425 c->c_listener_url.bv_val = NULL;
426 c->c_listener_url.bv_len = 0;
427 c->c_peer_domain.bv_val = NULL;
428 c->c_peer_domain.bv_len = 0;
429 c->c_peer_name.bv_val = NULL;
430 c->c_peer_name.bv_len = 0;
431 c->c_sock_name.bv_val = NULL;
432 c->c_sock_name.bv_len = 0;
434 LDAP_STAILQ_INIT(&c->c_ops);
435 LDAP_STAILQ_INIT(&c->c_pending_ops);
437 c->c_sasl_bind_mech.bv_val = NULL;
438 c->c_sasl_bind_mech.bv_len = 0;
439 c->c_sasl_context = NULL;
440 c->c_sasl_extra = NULL;
442 c->c_sb = ber_sockbuf_alloc( );
445 ber_len_t max = sockbuf_max_incoming;
446 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
449 c->c_currentber = NULL;
451 /* should check status of thread calls */
452 ldap_pvt_thread_mutex_init( &c->c_mutex );
453 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
454 ldap_pvt_thread_cond_init( &c->c_write_cv );
456 c->c_struct_state = SLAP_C_UNUSED;
459 ldap_pvt_thread_mutex_lock( &c->c_mutex );
461 assert( c->c_struct_state == SLAP_C_UNUSED );
462 assert( c->c_authmech.bv_val == NULL );
463 assert( c->c_dn.bv_val == NULL );
464 assert( c->c_ndn.bv_val == NULL );
465 assert( c->c_cdn.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_currentber == NULL );
478 ber_str2bv( url, 0, 1, &c->c_listener_url );
479 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
480 ber_str2bv( peername, 0, 1, &c->c_peer_name );
481 ber_str2bv( sockname, 0, 1, &c->c_sock_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)
509 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
510 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
512 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
513 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
514 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
515 LBER_SBIOD_LEVEL_PROVIDER, NULL );
520 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
521 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
523 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
524 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
528 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
529 INT_MAX, (void*)"ldap_" );
532 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
533 c /* non-NULL */ ) < 0 )
536 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
537 "connection_init: conn %lu set nonblocking failed\n",
540 Debug( LDAP_DEBUG_ANY,
541 "connection_init(%d, %s): set nonblocking failed\n",
542 s, c->c_peer_name.bv_val, 0 );
546 id = c->c_connid = conn_nextid++;
548 c->c_conn_state = SLAP_C_INACTIVE;
549 c->c_struct_state = SLAP_C_USED;
551 c->c_ssf = c->c_transport_ssf = ssf;
555 if ( tls_udp_option == 1 ) {
557 c->c_needs_tls_accept = 1;
560 c->c_needs_tls_accept = 0;
565 slap_sasl_external( c, ssf, authid );
567 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
568 ldap_pvt_thread_mutex_unlock( &connections_mutex );
570 backend_connection_init(c);
575 void connection2anonymous( Connection *c )
577 assert( connections != NULL );
581 ber_len_t max = sockbuf_max_incoming;
582 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
585 if(c->c_authmech.bv_val != NULL ) {
586 free(c->c_authmech.bv_val);
587 c->c_authmech.bv_val = NULL;
589 c->c_authmech.bv_len = 0;
591 if(c->c_dn.bv_val != NULL) {
592 free(c->c_dn.bv_val);
593 c->c_dn.bv_val = NULL;
596 if(c->c_ndn.bv_val != NULL) {
597 free(c->c_ndn.bv_val);
598 c->c_ndn.bv_val = NULL;
602 if(c->c_cdn.bv_val != NULL) {
603 free(c->c_cdn.bv_val);
604 c->c_cdn.bv_val = NULL;
608 c->c_authc_backend = NULL;
609 c->c_authz_backend = NULL;
612 GroupAssertion *g, *n;
613 for (g = c->c_groups; g; g=n)
624 connection_destroy( Connection *c )
626 /* note: connections_mutex should be locked by caller */
628 unsigned long connid;
630 assert( connections != NULL );
632 assert( c->c_struct_state != SLAP_C_UNUSED );
633 assert( c->c_conn_state != SLAP_C_INVALID );
634 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
636 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
637 connid = c->c_connid;
639 backend_connection_destroy(c);
644 c->c_activitytime = c->c_starttime = 0;
646 connection2anonymous( c );
648 if(c->c_listener_url.bv_val != NULL) {
649 free(c->c_listener_url.bv_val);
650 c->c_listener_url.bv_val = NULL;
652 c->c_listener_url.bv_len = 0;
654 if(c->c_peer_domain.bv_val != NULL) {
655 free(c->c_peer_domain.bv_val);
656 c->c_peer_domain.bv_val = NULL;
658 c->c_peer_domain.bv_len = 0;
659 if(c->c_peer_name.bv_val != NULL) {
662 * If peer was a domain socket, unlink. Mind you,
663 * they may be un-named. Should we leave this to
666 if (strncmp(c->c_peer_name.bv_val, "PATH=",
667 sizeof("PATH=") - 1) == 0) {
668 char *path = c->c_peer_name.bv_val
669 + sizeof("PATH=") - 1;
670 if (path[0] != '\0') {
674 #endif /* LDAP_PF_LOCAL */
676 free(c->c_peer_name.bv_val);
677 c->c_peer_name.bv_val = NULL;
679 c->c_peer_name.bv_len = 0;
680 if(c->c_sock_name.bv_val != NULL) {
681 free(c->c_sock_name.bv_val);
682 c->c_sock_name.bv_val = NULL;
684 c->c_sock_name.bv_len = 0;
686 c->c_sasl_bind_in_progress = 0;
687 if(c->c_sasl_bind_mech.bv_val != NULL) {
688 free(c->c_sasl_bind_mech.bv_val);
689 c->c_sasl_bind_mech.bv_val = NULL;
691 c->c_sasl_bind_mech.bv_len = 0;
693 slap_sasl_close( c );
695 if ( c->c_currentber != NULL ) {
696 ber_free( c->c_currentber, 1 );
697 c->c_currentber = NULL;
700 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
701 if ( sd != AC_SOCKET_INVALID ) {
702 slapd_remove( sd, 0 );
704 Statslog( LDAP_DEBUG_STATS,
705 "conn=%lu fd=%d closed\n",
706 connid, sd, 0, 0, 0 );
709 ber_sockbuf_free( c->c_sb );
711 c->c_sb = ber_sockbuf_alloc( );
714 ber_len_t max = sockbuf_max_incoming;
715 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
718 c->c_conn_state = SLAP_C_INVALID;
719 c->c_struct_state = SLAP_C_UNUSED;
722 int connection_state_closing( Connection *c )
724 /* c_mutex must be locked by caller */
728 assert( c->c_struct_state == SLAP_C_USED );
730 state = c->c_conn_state;
732 assert( state != SLAP_C_INVALID );
734 return state == SLAP_C_CLOSING;
737 static void connection_abandon( Connection *c )
739 /* c_mutex must be locked by caller */
743 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
744 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
746 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
749 /* remove pending operations */
750 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
751 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
752 LDAP_STAILQ_NEXT(o, o_next) = NULL;
757 void connection_closing( Connection *c )
759 assert( connections != NULL );
761 assert( c->c_struct_state == SLAP_C_USED );
762 assert( c->c_conn_state != SLAP_C_INVALID );
764 /* c_mutex must be locked by caller */
766 if( c->c_conn_state != SLAP_C_CLOSING ) {
769 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
771 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
772 "connection_closing: conn %lu readying socket %d for close.\n",
775 Debug( LDAP_DEBUG_TRACE,
776 "connection_closing: readying conn=%lu sd=%d for close\n",
777 c->c_connid, sd, 0 );
779 /* update state to closing */
780 c->c_conn_state = SLAP_C_CLOSING;
782 /* don't listen on this port anymore */
783 slapd_clr_read( sd, 1 );
785 /* abandon active operations */
786 connection_abandon( c );
788 /* wake write blocked operations */
789 slapd_clr_write( sd, 1 );
790 ldap_pvt_thread_cond_signal( &c->c_write_cv );
794 static void connection_close( Connection *c )
798 assert( connections != NULL );
800 assert( c->c_struct_state == SLAP_C_USED );
801 assert( c->c_conn_state == SLAP_C_CLOSING );
803 /* note: connections_mutex and c_mutex should be locked by caller */
805 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
806 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
808 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
809 "connection_close: conn %lu deferring sd %d\n",
812 Debug( LDAP_DEBUG_TRACE,
813 "connection_close: deferring conn=%lu sd=%d\n",
814 c->c_connid, sd, 0 );
820 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
821 "connection_close: conn %lu sd %d\n",
824 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
825 c->c_connid, sd, 0 );
827 connection_destroy( c );
830 unsigned long connections_nextid(void)
833 assert( connections != NULL );
835 ldap_pvt_thread_mutex_lock( &connections_mutex );
839 ldap_pvt_thread_mutex_unlock( &connections_mutex );
844 Connection* connection_first( ber_socket_t *index )
846 assert( connections != NULL );
847 assert( index != NULL );
849 ldap_pvt_thread_mutex_lock( &connections_mutex );
853 return connection_next(NULL, index);
856 Connection* connection_next( Connection *c, ber_socket_t *index )
858 assert( connections != NULL );
859 assert( index != NULL );
860 assert( *index <= dtblsize );
863 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
868 for(; *index < dtblsize; (*index)++) {
869 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
870 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
878 if( connections[*index].c_struct_state == SLAP_C_USED ) {
879 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
880 c = &connections[(*index)++];
884 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
885 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
889 ldap_pvt_thread_mutex_lock( &c->c_mutex );
895 void connection_done( Connection *c )
897 assert( connections != NULL );
900 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
903 ldap_pvt_thread_mutex_unlock( &connections_mutex );
907 * connection_activity - handle the request operation op on connection
908 * conn. This routine figures out what kind of operation it is and
909 * calls the appropriate stub to handle it.
913 #define INCR_OP(var,index) \
915 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
917 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
919 #else /* !SLAPD_MONITOR */
920 #define INCR_OP(var,index)
921 #endif /* !SLAPD_MONITOR */
924 connection_operation( void *arg_v )
927 struct co_arg *arg = arg_v;
928 ber_tag_t tag = arg->co_op->o_tag;
930 ber_tag_t oldtag = tag;
931 #endif /* SLAPD_MONITOR */
932 Connection *conn = arg->co_conn;
934 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
936 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
938 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
940 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
941 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
942 conn->c_connid, (long)tag ));
944 Debug( LDAP_DEBUG_ANY, "connection_operation: "
945 "error: SASL bind in progress (tag=%ld).\n",
948 send_ldap_result( conn, arg->co_op,
949 rc = LDAP_OPERATIONS_ERROR,
950 NULL, "SASL bind in progress", NULL, NULL );
951 goto operations_error;
956 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
957 rc = do_bind( conn, arg->co_op );
960 case LDAP_REQ_UNBIND:
961 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
962 rc = do_unbind( conn, arg->co_op );
966 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
967 rc = do_add( conn, arg->co_op );
970 case LDAP_REQ_DELETE:
971 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
972 rc = do_delete( conn, arg->co_op );
975 case LDAP_REQ_MODRDN:
976 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
977 rc = do_modrdn( conn, arg->co_op );
980 case LDAP_REQ_MODIFY:
981 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
982 rc = do_modify( conn, arg->co_op );
985 case LDAP_REQ_COMPARE:
986 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
987 rc = do_compare( conn, arg->co_op );
990 case LDAP_REQ_SEARCH:
991 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
992 rc = do_search( conn, arg->co_op );
995 case LDAP_REQ_ABANDON:
996 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
997 rc = do_abandon( conn, arg->co_op );
1000 case LDAP_REQ_EXTENDED:
1001 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
1002 rc = do_extended( conn, arg->co_op );
1007 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1008 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
1009 conn->c_connid, tag ));
1011 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
1014 arg->co_op->o_tag = LBER_ERROR;
1015 send_ldap_disconnect( conn, arg->co_op,
1016 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
1021 #ifdef SLAPD_MONITOR
1023 #endif /* SLAPD_MONITOR */
1024 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1027 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1028 num_ops_completed++;
1029 #ifdef SLAPD_MONITOR
1032 num_ops_completed_[SLAP_OP_BIND]++;
1034 case LDAP_REQ_UNBIND:
1035 num_ops_completed_[SLAP_OP_UNBIND]++;
1038 num_ops_completed_[SLAP_OP_ADD]++;
1040 case LDAP_REQ_DELETE:
1041 num_ops_completed_[SLAP_OP_DELETE]++;
1043 case LDAP_REQ_MODRDN:
1044 num_ops_completed_[SLAP_OP_MODRDN]++;
1046 case LDAP_REQ_MODIFY:
1047 num_ops_completed_[SLAP_OP_MODIFY]++;
1049 case LDAP_REQ_COMPARE:
1050 num_ops_completed_[SLAP_OP_COMPARE]++;
1052 case LDAP_REQ_SEARCH:
1053 num_ops_completed_[SLAP_OP_SEARCH]++;
1055 case LDAP_REQ_ABANDON:
1056 num_ops_completed_[SLAP_OP_ABANDON]++;
1058 case LDAP_REQ_EXTENDED:
1059 num_ops_completed_[SLAP_OP_EXTENDED]++;
1062 #endif /* SLAPD_MONITOR */
1063 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1065 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1067 conn->c_n_ops_executing--;
1068 conn->c_n_ops_completed++;
1070 LDAP_STAILQ_REMOVE( &conn->c_ops, arg->co_op, slap_op, o_next);
1071 LDAP_STAILQ_NEXT(arg->co_op, o_next) = NULL;
1072 slap_op_free( arg->co_op );
1074 arg->co_conn = NULL;
1075 free( (char *) arg );
1080 case LDAP_REQ_UNBIND:
1081 /* c_mutex is locked */
1082 connection_closing( conn );
1086 conn->c_sasl_bind_in_progress =
1087 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1089 if( conn->c_conn_state == SLAP_C_BINDING) {
1090 conn->c_conn_state = SLAP_C_ACTIVE;
1094 connection_resched( conn );
1096 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1101 int connection_read(ber_socket_t s)
1105 assert( connections != NULL );
1107 ldap_pvt_thread_mutex_lock( &connections_mutex );
1109 /* get (locked) connection */
1110 c = connection_get( s );
1114 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1115 "connection_read: sock %ld no connection\n",
1118 Debug( LDAP_DEBUG_ANY,
1119 "connection_read(%ld): no connection!\n",
1124 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1130 if( c->c_conn_state == SLAP_C_CLOSING ) {
1132 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1133 "connection_read: conn %lu connection closing, ignoring input\n",
1136 Debug( LDAP_DEBUG_TRACE,
1137 "connection_read(%d): closing, ignoring input for id=%lu\n",
1138 s, c->c_connid, 0 );
1140 connection_return( c );
1141 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1146 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1147 "connection_read: conn %lu checking for input.\n", c->c_connid ));
1149 Debug( LDAP_DEBUG_TRACE,
1150 "connection_read(%d): checking for input on id=%lu\n",
1151 s, c->c_connid, 0 );
1155 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1156 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1158 #if 0 /* required by next #if 0 */
1164 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1165 "connection_read: conn %lu TLS accept error, error %d\n",
1168 Debug( LDAP_DEBUG_TRACE,
1169 "connection_read(%d): TLS accept error "
1170 "error=%d id=%lu, closing\n",
1171 s, rc, c->c_connid );
1173 c->c_needs_tls_accept = 0;
1174 /* connections_mutex and c_mutex are locked */
1175 connection_closing( c );
1178 /* Drain input before close, to allow SSL error codes
1179 * to propagate to client. */
1186 rc = select(s+1, &rfd, NULL, NULL, &tv);
1188 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
1192 connection_close( c );
1194 } else if ( rc == 0 ) {
1198 c->c_needs_tls_accept = 0;
1200 /* we need to let SASL know */
1201 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1203 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1204 if( c->c_tls_ssf > c->c_ssf ) {
1205 c->c_ssf = c->c_tls_ssf;
1208 authid = (char *)ldap_pvt_tls_get_peer( ssl );
1209 slap_sasl_external( c, c->c_tls_ssf, authid );
1211 connection_return( c );
1212 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1217 #ifdef HAVE_CYRUS_SASL
1218 if ( c->c_sasl_layers ) {
1219 c->c_sasl_layers = 0;
1221 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1223 if( rc != LDAP_SUCCESS ) {
1225 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1226 "connection_read: conn %lu SASL install error %d, closing\n",
1229 Debug( LDAP_DEBUG_TRACE,
1230 "connection_read(%d): SASL install error "
1231 "error=%d id=%lu, closing\n",
1232 s, rc, c->c_connid );
1234 /* connections_mutex and c_mutex are locked */
1235 connection_closing( c );
1236 connection_close( c );
1237 connection_return( c );
1238 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1244 #define CONNECTION_INPUT_LOOP 1
1246 #ifdef DATA_READY_LOOP
1247 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1248 #elif CONNECTION_INPUT_LOOP
1252 /* How do we do this without getting into a busy loop ? */
1253 rc = connection_input( c );
1258 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1259 "connection_read: conn %lu input error %d, closing.\n",
1262 Debug( LDAP_DEBUG_TRACE,
1263 "connection_read(%d): input error=%d id=%lu, closing.\n",
1264 s, rc, c->c_connid );
1266 /* connections_mutex and c_mutex are locked */
1267 connection_closing( c );
1268 connection_close( c );
1269 connection_return( c );
1270 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1274 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1275 slapd_set_read( s, 1 );
1278 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1279 slapd_set_write( s, 1 );
1282 connection_return( c );
1283 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1297 #ifdef LDAP_CONNECTIONLESS
1302 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1305 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1306 "connection_input: conn %lu ber_alloc failed.\n",
1309 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1316 #ifdef LDAP_CONNECTIONLESS
1319 char peername[sizeof("IP=255.255.255.255:65336")];
1320 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1321 sizeof(struct sockaddr));
1322 if (len != sizeof(struct sockaddr))
1324 sprintf( peername, "IP=%s:%d",
1325 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1326 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1327 Statslog( LDAP_DEBUG_STATS,
1328 "conn=%lu UDP request from %s (%s) accepted.\n",
1329 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1332 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1333 if ( tag != LDAP_TAG_MESSAGE ) {
1337 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1340 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1341 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1342 conn->c_connid, err, sock_errstr(err) ));
1344 Debug( LDAP_DEBUG_TRACE,
1345 "ber_get_next on fd %d failed errno=%d (%s)\n",
1346 sd, err, sock_errstr(err) );
1348 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1349 /* log, close and send error */
1350 ber_free( conn->c_currentber, 1 );
1351 conn->c_currentber = NULL;
1358 ber = conn->c_currentber;
1359 conn->c_currentber = NULL;
1361 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1362 /* log, close and send error */
1364 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1365 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1366 conn->c_connid, tag ));
1368 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1375 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1376 /* log, close and send error */
1378 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1379 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1380 conn->c_connid, tag ));
1382 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1390 #ifdef LDAP_CONNECTIONLESS
1391 if (conn->c_is_udp) {
1392 if (tag == LBER_OCTETSTRING) {
1393 ber_get_stringa( ber, &cdn );
1394 tag = ber_peek_tag(ber, &len);
1396 if (tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH) {
1398 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1399 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1400 conn->c_connid, tag ));
1402 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1410 if(tag == LDAP_REQ_BIND) {
1411 /* immediately abandon all exiting operations upon BIND */
1412 connection_abandon( conn );
1415 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1417 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;
1426 if ( conn->c_conn_state == SLAP_C_BINDING
1427 || conn->c_conn_state == SLAP_C_CLOSING )
1430 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1431 "connection_input: conn %lu deferring operation\n",
1434 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1436 conn->c_n_ops_pending++;
1437 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1440 conn->c_n_ops_executing++;
1441 connection_op_activate( conn, op );
1445 if ( conn->c_struct_state != SLAP_C_USED ) {
1446 /* connection must have got closed underneath us */
1450 assert( conn->c_struct_state == SLAP_C_USED );
1456 connection_resched( Connection *conn )
1460 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1463 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1465 /* us trylock to avoid possible deadlock */
1466 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1470 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1471 "connection_resched: conn %lu reaquiring locks.\n",
1474 Debug( LDAP_DEBUG_TRACE,
1475 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1476 conn->c_connid, sd, 0 );
1479 * reaquire locks in the right order...
1480 * this may allow another thread to close this connection,
1481 * so recheck state below.
1483 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1484 ldap_pvt_thread_mutex_lock( &connections_mutex );
1485 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1488 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1490 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1491 "connection_resched: conn %lu closed by other thread.\n",
1494 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1495 "closed by other thread conn=%lu sd=%d\n",
1496 conn->c_connid, sd, 0 );
1500 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1501 "connection_resched: conn %lu attempting closing.\n",
1504 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1505 "attempting closing conn=%lu sd=%d\n",
1506 conn->c_connid, sd, 0 );
1508 connection_close( conn );
1511 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1515 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1516 /* other states need different handling */
1520 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1521 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1522 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1523 /* pending operations should not be marked for abandonment */
1524 assert(!op->o_abandon);
1526 conn->c_n_ops_pending--;
1527 conn->c_n_ops_executing++;
1529 connection_op_activate( conn, op );
1531 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1538 static int connection_op_activate( Connection *conn, Operation *op )
1542 ber_tag_t tag = op->o_tag;
1544 if(tag == LDAP_REQ_BIND) {
1545 conn->c_conn_state = SLAP_C_BINDING;
1548 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1549 arg->co_conn = conn;
1552 if (!arg->co_op->o_dn.bv_len) {
1553 arg->co_op->o_authz = conn->c_authz;
1554 arg->co_op->o_dn.bv_val = ch_strdup( conn->c_dn.bv_val ?
1555 conn->c_dn.bv_val : "" );
1556 arg->co_op->o_ndn.bv_val = ch_strdup( conn->c_ndn.bv_val ?
1557 conn->c_ndn.bv_val : "" );
1559 arg->co_op->o_authtype = conn->c_authtype;
1560 ber_dupbv( &arg->co_op->o_authmech, &conn->c_authmech );
1562 if (!arg->co_op->o_protocol) {
1563 arg->co_op->o_protocol = conn->c_protocol
1564 ? conn->c_protocol : LDAP_VERSION3;
1566 arg->co_op->o_connid = conn->c_connid;
1568 LDAP_STAILQ_INSERT_TAIL( &conn->c_ops, arg->co_op, o_next );
1570 status = ldap_pvt_thread_pool_submit( &connection_pool,
1571 connection_operation, (void *) arg );
1573 if ( status != 0 ) {
1575 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1576 "connection_op_activate: conn %lu thread pool submit failed.\n",
1579 Debug( LDAP_DEBUG_ANY,
1580 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1582 /* should move op to pending list */
1588 int connection_write(ber_socket_t s)
1591 assert( connections != NULL );
1593 ldap_pvt_thread_mutex_lock( &connections_mutex );
1595 c = connection_get( s );
1597 slapd_clr_write( s, 0);
1601 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1602 "connection_write: sock %ld no connection!\n",(long)s));
1604 Debug( LDAP_DEBUG_ANY,
1605 "connection_write(%ld): no connection!\n",
1609 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1616 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1617 "connection_write conn %lu waking output.\n",
1620 Debug( LDAP_DEBUG_TRACE,
1621 "connection_write(%d): waking output for id=%lu\n",
1622 s, c->c_connid, 0 );
1624 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1626 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1627 slapd_set_read( s, 1 );
1628 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1629 slapd_set_write( s, 1 );
1630 connection_return( c );
1631 ldap_pvt_thread_mutex_unlock( &connections_mutex );