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 );
69 * Initialize connection management infrastructure.
71 int connections_init(void)
73 assert( connections == NULL );
75 if( connections != NULL) {
77 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
78 "connections_init: already initialized.\n" ));
80 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
86 /* should check return of every call */
87 ldap_pvt_thread_mutex_init( &connections_mutex );
89 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
91 if( connections == NULL ) {
93 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
94 "connections_init: allocation (%d * %ld) of connection array failed\n",
95 dtblsize, (long) sizeof(Connection) ));
97 Debug( LDAP_DEBUG_ANY,
98 "connections_init: allocation (%d*%ld) of connection array failed\n",
99 dtblsize, (long) sizeof(Connection), 0 );
104 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
105 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
108 * per entry initialization of the Connection array initialization
109 * will be done by connection_init()
116 * Destroy connection management infrastructure.
118 int connections_destroy(void)
122 /* should check return of every call */
124 if( connections == NULL) {
126 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
127 "connections_destroy: nothing to destroy.\n"));
129 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
135 for ( i = 0; i < dtblsize; i++ ) {
136 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
137 ber_sockbuf_free( connections[i].c_sb );
138 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
139 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
140 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
147 ldap_pvt_thread_mutex_destroy( &connections_mutex );
152 * shutdown all connections
154 int connections_shutdown(void)
158 ldap_pvt_thread_mutex_lock( &connections_mutex );
160 for ( i = 0; i < dtblsize; i++ ) {
161 if( connections[i].c_struct_state != SLAP_C_USED ) {
165 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
167 /* connections_mutex and c_mutex are locked */
168 connection_closing( &connections[i] );
169 connection_close( &connections[i] );
171 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
174 ldap_pvt_thread_mutex_unlock( &connections_mutex );
180 * Timeout idle connections.
182 int connections_timeout_idle(time_t now)
188 for( c = connection_first( &connindex );
190 c = connection_next( c, &connindex ) )
192 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
194 connection_closing( c );
195 connection_close( c );
199 connection_done( c );
204 static Connection* connection_get( ber_socket_t s )
206 /* connections_mutex should be locked by caller */
211 LDAP_LOG(( "connection", LDAP_LEVEL_ENTRY,
212 "connection_get: socket %ld\n", (long)s ));
214 Debug( LDAP_DEBUG_ARGS,
215 "connection_get(%ld)\n",
219 assert( connections != NULL );
221 if(s == AC_SOCKET_INVALID) {
228 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
235 for(i=0; i<dtblsize; i++) {
236 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
237 assert( connections[i].c_conn_state == SLAP_C_INVALID );
238 assert( connections[i].c_sb == 0 );
242 ber_sockbuf_ctrl( connections[i].c_sb,
243 LBER_SB_OPT_GET_FD, &sd );
245 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
246 assert( connections[i].c_conn_state == SLAP_C_INVALID );
247 assert( sd == AC_SOCKET_INVALID );
251 /* state can actually change from used -> unused by resched,
252 * so don't assert details here.
266 ldap_pvt_thread_mutex_lock( &c->c_mutex );
268 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
269 if( c->c_struct_state != SLAP_C_USED ) {
270 /* connection must have been closed due to resched */
272 assert( c->c_conn_state == SLAP_C_INVALID );
273 assert( sd == AC_SOCKET_INVALID );
276 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
277 "connection_get: connection %d not used\n", s ));
279 Debug( LDAP_DEBUG_TRACE,
280 "connection_get(%d): connection not used\n",
284 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
289 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
290 "connection_get: get for %d got connid %lu\n", s, c->c_connid ));
292 Debug( LDAP_DEBUG_TRACE,
293 "connection_get(%d): got connid=%lu\n",
299 assert( c->c_struct_state == SLAP_C_USED );
300 assert( c->c_conn_state != SLAP_C_INVALID );
301 assert( sd != AC_SOCKET_INVALID );
304 c->c_activitytime = slap_get_time();
306 if( global_idletimeout > 0 ) {
307 c->c_activitytime = slap_get_time();
315 static void connection_return( Connection *c )
317 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
320 long connection_init(
324 const char* peername,
325 const char* sockname,
333 assert( connections != NULL );
335 assert( dnsname != NULL );
336 assert( peername != NULL );
337 assert( sockname != NULL );
340 assert( tls_udp_option != 1 );
343 if( s == AC_SOCKET_INVALID ) {
345 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
346 "connection_init: init of socket %ld invalid.\n", (long)s ));
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", LDAP_LEVEL_INFO,
398 "connection_init: skt %d connection table full (%d/%d)\n",
401 Debug( LDAP_DEBUG_ANY,
402 "connection_init(%d): connection table full (%d/%d)\n",
405 ldap_pvt_thread_mutex_unlock( &connections_mutex );
413 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
414 c->c_authmech.bv_val = NULL;
415 c->c_authmech.bv_len = 0;
416 c->c_dn.bv_val = NULL;
418 c->c_ndn.bv_val = NULL;
420 c->c_cdn.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;
441 c->c_sb = ber_sockbuf_alloc( );
444 ber_len_t max = sockbuf_max_incoming;
445 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
448 c->c_currentber = NULL;
450 /* should check status of thread calls */
451 ldap_pvt_thread_mutex_init( &c->c_mutex );
452 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
453 ldap_pvt_thread_cond_init( &c->c_write_cv );
455 c->c_struct_state = SLAP_C_UNUSED;
458 ldap_pvt_thread_mutex_lock( &c->c_mutex );
460 assert( c->c_struct_state == SLAP_C_UNUSED );
461 assert( c->c_authmech.bv_val == NULL );
462 assert( c->c_dn.bv_val == NULL );
463 assert( c->c_ndn.bv_val == NULL );
464 assert( c->c_cdn.bv_val == NULL );
465 assert( c->c_groups == NULL );
466 assert( c->c_listener_url.bv_val == NULL );
467 assert( c->c_peer_domain.bv_val == NULL );
468 assert( c->c_peer_name.bv_val == NULL );
469 assert( c->c_sock_name.bv_val == NULL );
470 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
471 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
472 assert( c->c_sasl_bind_mech.bv_val == NULL );
473 assert( c->c_sasl_context == NULL );
474 assert( c->c_sasl_extra == NULL );
475 assert( c->c_currentber == NULL );
477 ber_str2bv( url, 0, 1, &c->c_listener_url );
478 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
479 ber_str2bv( peername, 0, 1, &c->c_peer_name );
480 ber_str2bv( sockname, 0, 1, &c->c_sock_name );
482 c->c_n_ops_received = 0;
483 c->c_n_ops_executing = 0;
484 c->c_n_ops_pending = 0;
485 c->c_n_ops_completed = 0;
491 /* set to zero until bind, implies LDAP_VERSION3 */
495 c->c_activitytime = c->c_starttime = slap_get_time();
497 if( global_idletimeout > 0 ) {
498 c->c_activitytime = c->c_starttime = slap_get_time();
502 #ifdef LDAP_CONNECTIONLESS
504 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", LDAP_LEVEL_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 if(c->c_cdn.bv_val != NULL) {
602 free(c->c_cdn.bv_val);
603 c->c_cdn.bv_val = NULL;
607 c->c_authz_backend = NULL;
610 GroupAssertion *g, *n;
611 for (g = c->c_groups; g; g=n)
622 connection_destroy( Connection *c )
624 /* note: connections_mutex should be locked by caller */
626 unsigned long connid;
628 assert( connections != NULL );
630 assert( c->c_struct_state != SLAP_C_UNUSED );
631 assert( c->c_conn_state != SLAP_C_INVALID );
632 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
634 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
635 connid = c->c_connid;
637 backend_connection_destroy(c);
642 c->c_activitytime = c->c_starttime = 0;
644 connection2anonymous( c );
646 if(c->c_listener_url.bv_val != NULL) {
647 free(c->c_listener_url.bv_val);
648 c->c_listener_url.bv_val = NULL;
650 c->c_listener_url.bv_len = 0;
652 if(c->c_peer_domain.bv_val != NULL) {
653 free(c->c_peer_domain.bv_val);
654 c->c_peer_domain.bv_val = NULL;
656 c->c_peer_domain.bv_len = 0;
657 if(c->c_peer_name.bv_val != NULL) {
660 * If peer was a domain socket, unlink. Mind you,
661 * they may be un-named. Should we leave this to
664 if (strncmp(c->c_peer_name.bv_val, "PATH=",
665 sizeof("PATH=") - 1) == 0) {
666 char *path = c->c_peer_name.bv_val
667 + sizeof("PATH=") - 1;
668 if (path[0] != '\0') {
672 #endif /* LDAP_PF_LOCAL */
674 free(c->c_peer_name.bv_val);
675 c->c_peer_name.bv_val = NULL;
677 c->c_peer_name.bv_len = 0;
678 if(c->c_sock_name.bv_val != NULL) {
679 free(c->c_sock_name.bv_val);
680 c->c_sock_name.bv_val = NULL;
682 c->c_sock_name.bv_len = 0;
684 c->c_sasl_bind_in_progress = 0;
685 if(c->c_sasl_bind_mech.bv_val != NULL) {
686 free(c->c_sasl_bind_mech.bv_val);
687 c->c_sasl_bind_mech.bv_val = NULL;
689 c->c_sasl_bind_mech.bv_len = 0;
691 slap_sasl_close( c );
693 if ( c->c_currentber != NULL ) {
694 ber_free( c->c_currentber, 1 );
695 c->c_currentber = NULL;
698 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
699 if ( sd != AC_SOCKET_INVALID ) {
700 slapd_remove( sd, 0 );
702 Statslog( LDAP_DEBUG_STATS,
703 "conn=%lu fd=%d closed\n",
704 connid, sd, 0, 0, 0 );
707 ber_sockbuf_free( c->c_sb );
709 c->c_sb = ber_sockbuf_alloc( );
712 ber_len_t max = sockbuf_max_incoming;
713 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
716 c->c_conn_state = SLAP_C_INVALID;
717 c->c_struct_state = SLAP_C_UNUSED;
720 int connection_state_closing( Connection *c )
722 /* c_mutex must be locked by caller */
726 assert( c->c_struct_state == SLAP_C_USED );
728 state = c->c_conn_state;
730 assert( state != SLAP_C_INVALID );
732 return state == SLAP_C_CLOSING;
735 static void connection_abandon( Connection *c )
737 /* c_mutex must be locked by caller */
741 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
745 /* remove pending operations */
746 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
747 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
748 LDAP_STAILQ_NEXT(o, o_next) = NULL;
753 void connection_closing( Connection *c )
755 assert( connections != NULL );
757 assert( c->c_struct_state == SLAP_C_USED );
758 assert( c->c_conn_state != SLAP_C_INVALID );
760 /* c_mutex must be locked by caller */
762 if( c->c_conn_state != SLAP_C_CLOSING ) {
765 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
767 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
768 "connection_closing: conn %lu readying socket %d for close.\n",
771 Debug( LDAP_DEBUG_TRACE,
772 "connection_closing: readying conn=%lu sd=%d for close\n",
773 c->c_connid, sd, 0 );
775 /* update state to closing */
776 c->c_conn_state = SLAP_C_CLOSING;
778 /* don't listen on this port anymore */
779 slapd_clr_read( sd, 1 );
781 /* abandon active operations */
782 connection_abandon( c );
784 /* wake write blocked operations */
785 slapd_clr_write( sd, 1 );
786 ldap_pvt_thread_cond_signal( &c->c_write_cv );
790 static void connection_close( Connection *c )
794 assert( connections != NULL );
796 assert( c->c_struct_state == SLAP_C_USED );
797 assert( c->c_conn_state == SLAP_C_CLOSING );
799 /* note: connections_mutex and c_mutex should be locked by caller */
801 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
802 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
804 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
805 "connection_close: conn %lu deferring sd %d\n",
808 Debug( LDAP_DEBUG_TRACE,
809 "connection_close: deferring conn=%lu sd=%d\n",
810 c->c_connid, sd, 0 );
816 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
817 "connection_close: conn %lu sd %d\n",
820 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
821 c->c_connid, sd, 0 );
823 connection_destroy( c );
826 unsigned long connections_nextid(void)
829 assert( connections != NULL );
831 ldap_pvt_thread_mutex_lock( &connections_mutex );
835 ldap_pvt_thread_mutex_unlock( &connections_mutex );
840 Connection* connection_first( ber_socket_t *index )
842 assert( connections != NULL );
843 assert( index != NULL );
845 ldap_pvt_thread_mutex_lock( &connections_mutex );
849 return connection_next(NULL, index);
852 Connection* connection_next( Connection *c, ber_socket_t *index )
854 assert( connections != NULL );
855 assert( index != NULL );
856 assert( *index <= dtblsize );
859 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
864 for(; *index < dtblsize; (*index)++) {
865 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
866 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
874 if( connections[*index].c_struct_state == SLAP_C_USED ) {
875 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
876 c = &connections[(*index)++];
880 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
881 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
885 ldap_pvt_thread_mutex_lock( &c->c_mutex );
891 void connection_done( Connection *c )
893 assert( connections != NULL );
896 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
899 ldap_pvt_thread_mutex_unlock( &connections_mutex );
903 * connection_activity - handle the request operation op on connection
904 * conn. This routine figures out what kind of operation it is and
905 * calls the appropriate stub to handle it.
909 #define INCR_OP(var,index) \
911 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
913 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
915 #else /* !SLAPD_MONITOR */
916 #define INCR_OP(var,index)
917 #endif /* !SLAPD_MONITOR */
920 connection_operation( void *arg_v )
923 struct co_arg *arg = arg_v;
924 ber_tag_t tag = arg->co_op->o_tag;
926 ber_tag_t oldtag = tag;
927 #endif /* SLAPD_MONITOR */
928 Connection *conn = arg->co_conn;
930 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
932 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
934 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
936 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
937 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
938 conn->c_connid, (long)tag ));
940 Debug( LDAP_DEBUG_ANY, "connection_operation: "
941 "error: SASL bind in progress (tag=%ld).\n",
944 send_ldap_result( conn, arg->co_op,
945 rc = LDAP_OPERATIONS_ERROR,
946 NULL, "SASL bind in progress", NULL, NULL );
947 goto operations_error;
952 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
953 rc = do_bind( conn, arg->co_op );
956 case LDAP_REQ_UNBIND:
957 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
958 rc = do_unbind( conn, arg->co_op );
962 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
963 rc = do_add( conn, arg->co_op );
966 case LDAP_REQ_DELETE:
967 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
968 rc = do_delete( conn, arg->co_op );
971 case LDAP_REQ_MODRDN:
972 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
973 rc = do_modrdn( conn, arg->co_op );
976 case LDAP_REQ_MODIFY:
977 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
978 rc = do_modify( conn, arg->co_op );
981 case LDAP_REQ_COMPARE:
982 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
983 rc = do_compare( conn, arg->co_op );
986 case LDAP_REQ_SEARCH:
987 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
988 rc = do_search( conn, arg->co_op );
991 case LDAP_REQ_ABANDON:
992 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
993 rc = do_abandon( conn, arg->co_op );
996 case LDAP_REQ_EXTENDED:
997 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
998 rc = do_extended( conn, arg->co_op );
1003 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1004 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
1005 conn->c_connid, tag ));
1007 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
1010 arg->co_op->o_tag = LBER_ERROR;
1011 send_ldap_disconnect( conn, arg->co_op,
1012 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
1017 #ifdef SLAPD_MONITOR
1019 #endif /* SLAPD_MONITOR */
1020 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1023 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1024 num_ops_completed++;
1025 #ifdef SLAPD_MONITOR
1028 num_ops_completed_[SLAP_OP_BIND]++;
1030 case LDAP_REQ_UNBIND:
1031 num_ops_completed_[SLAP_OP_UNBIND]++;
1034 num_ops_completed_[SLAP_OP_ADD]++;
1036 case LDAP_REQ_DELETE:
1037 num_ops_completed_[SLAP_OP_DELETE]++;
1039 case LDAP_REQ_MODRDN:
1040 num_ops_completed_[SLAP_OP_MODRDN]++;
1042 case LDAP_REQ_MODIFY:
1043 num_ops_completed_[SLAP_OP_MODIFY]++;
1045 case LDAP_REQ_COMPARE:
1046 num_ops_completed_[SLAP_OP_COMPARE]++;
1048 case LDAP_REQ_SEARCH:
1049 num_ops_completed_[SLAP_OP_SEARCH]++;
1051 case LDAP_REQ_ABANDON:
1052 num_ops_completed_[SLAP_OP_ABANDON]++;
1054 case LDAP_REQ_EXTENDED:
1055 num_ops_completed_[SLAP_OP_EXTENDED]++;
1058 #endif /* SLAPD_MONITOR */
1059 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1061 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1063 conn->c_n_ops_executing--;
1064 conn->c_n_ops_completed++;
1066 LDAP_STAILQ_REMOVE( &conn->c_ops, arg->co_op, slap_op, o_next);
1067 LDAP_STAILQ_NEXT(arg->co_op, o_next) = NULL;
1068 slap_op_free( arg->co_op );
1070 arg->co_conn = NULL;
1071 free( (char *) arg );
1076 case LDAP_REQ_UNBIND:
1077 /* c_mutex is locked */
1078 connection_closing( conn );
1082 conn->c_sasl_bind_in_progress =
1083 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1085 if( conn->c_conn_state == SLAP_C_BINDING) {
1086 conn->c_conn_state = SLAP_C_ACTIVE;
1090 connection_resched( conn );
1092 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1097 int connection_read(ber_socket_t s)
1101 assert( connections != NULL );
1103 ldap_pvt_thread_mutex_lock( &connections_mutex );
1105 /* get (locked) connection */
1106 c = connection_get( s );
1110 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1111 "connection_read: sock %ld no connection\n",
1114 Debug( LDAP_DEBUG_ANY,
1115 "connection_read(%ld): no connection!\n",
1120 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1126 if( c->c_conn_state == SLAP_C_CLOSING ) {
1128 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1129 "connection_read: conn %lu connection closing, ignoring input\n",
1132 Debug( LDAP_DEBUG_TRACE,
1133 "connection_read(%d): closing, ignoring input for id=%lu\n",
1134 s, c->c_connid, 0 );
1136 connection_return( c );
1137 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1142 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1143 "connection_read: conn %lu checking for input.\n", c->c_connid ));
1145 Debug( LDAP_DEBUG_TRACE,
1146 "connection_read(%d): checking for input on id=%lu\n",
1147 s, c->c_connid, 0 );
1151 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1152 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1154 #if 0 /* required by next #if 0 */
1160 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1161 "connection_read: conn %lu TLS accept error, error %d\n",
1164 Debug( LDAP_DEBUG_TRACE,
1165 "connection_read(%d): TLS accept error "
1166 "error=%d id=%lu, closing\n",
1167 s, rc, c->c_connid );
1169 c->c_needs_tls_accept = 0;
1170 /* connections_mutex and c_mutex are locked */
1171 connection_closing( c );
1174 /* Drain input before close, to allow SSL error codes
1175 * to propagate to client. */
1182 rc = select(s+1, &rfd, NULL, NULL, &tv);
1184 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
1188 connection_close( c );
1190 } else if ( rc == 0 ) {
1192 struct berval authid;
1194 c->c_needs_tls_accept = 0;
1196 /* we need to let SASL know */
1197 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1199 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1200 if( c->c_tls_ssf > c->c_ssf ) {
1201 c->c_ssf = c->c_tls_ssf;
1204 rc = dnX509peerNormalize( ssl, &authid );
1205 if ( rc != LDAP_SUCCESS ) {
1207 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1208 "connection_read: conn %lu unable to get TLS client DN, error %d\n",
1211 Debug( LDAP_DEBUG_TRACE,
1212 "connection_read(%d): unable to get TLS client DN "
1213 "error=%d id=%lu\n",
1214 s, rc, c->c_connid );
1217 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1218 if ( authid.bv_val ) free( authid.bv_val );
1220 connection_return( c );
1221 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1226 #ifdef HAVE_CYRUS_SASL
1227 if ( c->c_sasl_layers ) {
1228 c->c_sasl_layers = 0;
1230 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1232 if( rc != LDAP_SUCCESS ) {
1234 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1235 "connection_read: conn %lu SASL install error %d, closing\n",
1238 Debug( LDAP_DEBUG_TRACE,
1239 "connection_read(%d): SASL install error "
1240 "error=%d id=%lu, closing\n",
1241 s, rc, c->c_connid );
1243 /* connections_mutex and c_mutex are locked */
1244 connection_closing( c );
1245 connection_close( c );
1246 connection_return( c );
1247 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1253 #define CONNECTION_INPUT_LOOP 1
1255 #ifdef DATA_READY_LOOP
1256 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1257 #elif CONNECTION_INPUT_LOOP
1261 /* How do we do this without getting into a busy loop ? */
1262 rc = connection_input( c );
1267 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1268 "connection_read: conn %lu input error %d, closing.\n",
1271 Debug( LDAP_DEBUG_TRACE,
1272 "connection_read(%d): input error=%d id=%lu, closing.\n",
1273 s, rc, c->c_connid );
1275 /* connections_mutex and c_mutex are locked */
1276 connection_closing( c );
1277 connection_close( c );
1278 connection_return( c );
1279 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1283 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1284 slapd_set_read( s, 1 );
1287 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1288 slapd_set_write( s, 1 );
1291 connection_return( c );
1292 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1306 #ifdef LDAP_CONNECTIONLESS
1311 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1314 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1315 "connection_input: conn %lu ber_alloc failed.\n",
1318 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1325 #ifdef LDAP_CONNECTIONLESS
1328 char peername[sizeof("IP=255.255.255.255:65336")];
1329 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1330 sizeof(struct sockaddr));
1331 if (len != sizeof(struct sockaddr))
1333 sprintf( peername, "IP=%s:%d",
1334 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1335 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1336 Statslog( LDAP_DEBUG_STATS,
1337 "conn=%lu UDP request from %s (%s) accepted.\n",
1338 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1341 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1342 if ( tag != LDAP_TAG_MESSAGE ) {
1346 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1349 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1350 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1351 conn->c_connid, err, sock_errstr(err) ));
1353 Debug( LDAP_DEBUG_TRACE,
1354 "ber_get_next on fd %d failed errno=%d (%s)\n",
1355 sd, err, sock_errstr(err) );
1357 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1358 /* log, close and send error */
1359 ber_free( conn->c_currentber, 1 );
1360 conn->c_currentber = NULL;
1367 ber = conn->c_currentber;
1368 conn->c_currentber = NULL;
1370 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1371 /* log, close and send error */
1373 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1374 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1375 conn->c_connid, tag ));
1377 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1384 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1385 /* log, close and send error */
1387 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1388 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1389 conn->c_connid, tag ));
1391 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1399 #ifdef LDAP_CONNECTIONLESS
1400 if (conn->c_is_udp) {
1401 if (tag == LBER_OCTETSTRING) {
1402 ber_get_stringa( ber, &cdn );
1403 tag = ber_peek_tag(ber, &len);
1405 if (tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH) {
1407 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1408 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1409 conn->c_connid, tag ));
1411 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1419 if(tag == LDAP_REQ_BIND) {
1420 /* immediately abandon all exiting operations upon BIND */
1421 connection_abandon( conn );
1424 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1426 op->vrFilter = NULL;
1428 op->o_pagedresults_state = conn->c_pagedresults_state;
1430 #ifdef LDAP_CONNECTIONLESS
1431 op->o_peeraddr = peeraddr;
1433 ber_str2bv( cdn, 0, 1, &op->o_dn );
1434 op->o_protocol = LDAP_VERSION2;
1437 if ( conn->c_conn_state == SLAP_C_BINDING
1438 || conn->c_conn_state == SLAP_C_CLOSING )
1441 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1442 "connection_input: conn %lu deferring operation\n",
1445 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1447 conn->c_n_ops_pending++;
1448 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1451 conn->c_n_ops_executing++;
1452 connection_op_activate( conn, op );
1456 if ( conn->c_struct_state != SLAP_C_USED ) {
1457 /* connection must have got closed underneath us */
1461 assert( conn->c_struct_state == SLAP_C_USED );
1467 connection_resched( Connection *conn )
1471 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1474 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1476 /* us trylock to avoid possible deadlock */
1477 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1481 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1482 "connection_resched: conn %lu reaquiring locks.\n",
1485 Debug( LDAP_DEBUG_TRACE,
1486 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1487 conn->c_connid, sd, 0 );
1490 * reaquire locks in the right order...
1491 * this may allow another thread to close this connection,
1492 * so recheck state below.
1494 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1495 ldap_pvt_thread_mutex_lock( &connections_mutex );
1496 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1499 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1501 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1502 "connection_resched: conn %lu closed by other thread.\n",
1505 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1506 "closed by other thread conn=%lu sd=%d\n",
1507 conn->c_connid, sd, 0 );
1511 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1512 "connection_resched: conn %lu attempting closing.\n",
1515 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1516 "attempting closing conn=%lu sd=%d\n",
1517 conn->c_connid, sd, 0 );
1519 connection_close( conn );
1522 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1526 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1527 /* other states need different handling */
1531 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1532 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1533 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1534 /* pending operations should not be marked for abandonment */
1535 assert(!op->o_abandon);
1537 conn->c_n_ops_pending--;
1538 conn->c_n_ops_executing++;
1540 connection_op_activate( conn, op );
1542 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1549 static int connection_op_activate( Connection *conn, Operation *op )
1553 ber_tag_t tag = op->o_tag;
1555 if(tag == LDAP_REQ_BIND) {
1556 conn->c_conn_state = SLAP_C_BINDING;
1559 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1560 arg->co_conn = conn;
1563 if (!arg->co_op->o_dn.bv_len) {
1564 arg->co_op->o_authz = conn->c_authz;
1565 arg->co_op->o_dn.bv_val = ch_strdup( conn->c_dn.bv_val ?
1566 conn->c_dn.bv_val : "" );
1567 arg->co_op->o_ndn.bv_val = ch_strdup( conn->c_ndn.bv_val ?
1568 conn->c_ndn.bv_val : "" );
1570 arg->co_op->o_authtype = conn->c_authtype;
1571 ber_dupbv( &arg->co_op->o_authmech, &conn->c_authmech );
1573 if (!arg->co_op->o_protocol) {
1574 arg->co_op->o_protocol = conn->c_protocol
1575 ? conn->c_protocol : LDAP_VERSION3;
1577 arg->co_op->o_connid = conn->c_connid;
1579 LDAP_STAILQ_INSERT_TAIL( &conn->c_ops, arg->co_op, o_next );
1581 status = ldap_pvt_thread_pool_submit( &connection_pool,
1582 connection_operation, (void *) arg );
1584 if ( status != 0 ) {
1586 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1587 "connection_op_activate: conn %lu thread pool submit failed.\n",
1590 Debug( LDAP_DEBUG_ANY,
1591 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1593 /* should move op to pending list */
1599 int connection_write(ber_socket_t s)
1602 assert( connections != NULL );
1604 ldap_pvt_thread_mutex_lock( &connections_mutex );
1606 c = connection_get( s );
1608 slapd_clr_write( s, 0);
1612 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1613 "connection_write: sock %ld no connection!\n",(long)s));
1615 Debug( LDAP_DEBUG_ANY,
1616 "connection_write(%ld): no connection!\n",
1620 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1627 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1628 "connection_write conn %lu waking output.\n",
1631 Debug( LDAP_DEBUG_TRACE,
1632 "connection_write(%d): waking output for id=%lu\n",
1633 s, c->c_connid, 0 );
1635 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1637 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1638 slapd_set_read( s, 1 );
1639 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1640 slapd_set_write( s, 1 );
1641 connection_return( c );
1642 ldap_pvt_thread_mutex_unlock( &connections_mutex );