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;
422 c->c_listener_url.bv_val = NULL;
423 c->c_listener_url.bv_len = 0;
424 c->c_peer_domain.bv_val = NULL;
425 c->c_peer_domain.bv_len = 0;
426 c->c_peer_name.bv_val = NULL;
427 c->c_peer_name.bv_len = 0;
428 c->c_sock_name.bv_val = NULL;
429 c->c_sock_name.bv_len = 0;
431 LDAP_STAILQ_INIT(&c->c_ops);
432 LDAP_STAILQ_INIT(&c->c_pending_ops);
434 c->c_sasl_bind_mech.bv_val = NULL;
435 c->c_sasl_bind_mech.bv_len = 0;
436 c->c_sasl_context = NULL;
437 c->c_sasl_extra = NULL;
439 c->c_sb = ber_sockbuf_alloc( );
442 ber_len_t max = sockbuf_max_incoming;
443 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
446 c->c_currentber = NULL;
448 /* should check status of thread calls */
449 ldap_pvt_thread_mutex_init( &c->c_mutex );
450 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
451 ldap_pvt_thread_cond_init( &c->c_write_cv );
453 c->c_struct_state = SLAP_C_UNUSED;
456 ldap_pvt_thread_mutex_lock( &c->c_mutex );
458 assert( c->c_struct_state == SLAP_C_UNUSED );
459 assert( c->c_authmech.bv_val == NULL );
460 assert( c->c_dn.bv_val == NULL );
461 assert( c->c_ndn.bv_val == NULL );
462 assert( c->c_groups == NULL );
463 assert( c->c_listener_url.bv_val == NULL );
464 assert( c->c_peer_domain.bv_val == NULL );
465 assert( c->c_peer_name.bv_val == NULL );
466 assert( c->c_sock_name.bv_val == NULL );
467 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
468 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
469 assert( c->c_sasl_bind_mech.bv_val == NULL );
470 assert( c->c_sasl_context == NULL );
471 assert( c->c_sasl_extra == NULL );
472 assert( c->c_currentber == NULL );
474 ber_str2bv( url, 0, 1, &c->c_listener_url );
475 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
476 ber_str2bv( peername, 0, 1, &c->c_peer_name );
477 ber_str2bv( sockname, 0, 1, &c->c_sock_name );
479 c->c_n_ops_received = 0;
480 c->c_n_ops_executing = 0;
481 c->c_n_ops_pending = 0;
482 c->c_n_ops_completed = 0;
488 /* set to zero until bind, implies LDAP_VERSION3 */
492 c->c_activitytime = c->c_starttime = slap_get_time();
494 if( global_idletimeout > 0 ) {
495 c->c_activitytime = c->c_starttime = slap_get_time();
499 #ifdef LDAP_CONNECTIONLESS
501 if (tls_udp_option == 2)
505 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
506 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
508 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
509 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
510 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
511 LBER_SBIOD_LEVEL_PROVIDER, NULL );
516 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
517 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
519 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
520 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
524 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
525 INT_MAX, (void*)"ldap_" );
528 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
529 c /* non-NULL */ ) < 0 )
532 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
533 "connection_init: conn %lu set nonblocking failed\n",
536 Debug( LDAP_DEBUG_ANY,
537 "connection_init(%d, %s): set nonblocking failed\n",
538 s, c->c_peer_name.bv_val, 0 );
542 id = c->c_connid = conn_nextid++;
544 c->c_conn_state = SLAP_C_INACTIVE;
545 c->c_struct_state = SLAP_C_USED;
547 c->c_ssf = c->c_transport_ssf = ssf;
551 if ( tls_udp_option == 1 ) {
553 c->c_needs_tls_accept = 1;
556 c->c_needs_tls_accept = 0;
561 slap_sasl_external( c, ssf, authid );
563 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
564 ldap_pvt_thread_mutex_unlock( &connections_mutex );
566 backend_connection_init(c);
571 void connection2anonymous( Connection *c )
573 assert( connections != NULL );
577 ber_len_t max = sockbuf_max_incoming;
578 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
581 if(c->c_authmech.bv_val != NULL ) {
582 free(c->c_authmech.bv_val);
583 c->c_authmech.bv_val = NULL;
585 c->c_authmech.bv_len = 0;
587 if(c->c_dn.bv_val != NULL) {
588 free(c->c_dn.bv_val);
589 c->c_dn.bv_val = NULL;
592 if(c->c_ndn.bv_val != NULL) {
593 free(c->c_ndn.bv_val);
594 c->c_ndn.bv_val = NULL;
598 c->c_authz_backend = NULL;
601 GroupAssertion *g, *n;
602 for (g = c->c_groups; g; g=n) {
611 connection_destroy( Connection *c )
613 /* note: connections_mutex should be locked by caller */
615 unsigned long connid;
617 assert( connections != NULL );
619 assert( c->c_struct_state != SLAP_C_UNUSED );
620 assert( c->c_conn_state != SLAP_C_INVALID );
621 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
623 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
624 connid = c->c_connid;
626 backend_connection_destroy(c);
631 c->c_activitytime = c->c_starttime = 0;
633 connection2anonymous( c );
635 if(c->c_listener_url.bv_val != NULL) {
636 free(c->c_listener_url.bv_val);
637 c->c_listener_url.bv_val = NULL;
639 c->c_listener_url.bv_len = 0;
641 if(c->c_peer_domain.bv_val != NULL) {
642 free(c->c_peer_domain.bv_val);
643 c->c_peer_domain.bv_val = NULL;
645 c->c_peer_domain.bv_len = 0;
646 if(c->c_peer_name.bv_val != NULL) {
649 * If peer was a domain socket, unlink. Mind you,
650 * they may be un-named. Should we leave this to
653 if (strncmp(c->c_peer_name.bv_val, "PATH=",
654 sizeof("PATH=") - 1) == 0) {
655 char *path = c->c_peer_name.bv_val
656 + sizeof("PATH=") - 1;
657 if (path[0] != '\0') {
661 #endif /* LDAP_PF_LOCAL */
663 free(c->c_peer_name.bv_val);
664 c->c_peer_name.bv_val = NULL;
666 c->c_peer_name.bv_len = 0;
667 if(c->c_sock_name.bv_val != NULL) {
668 free(c->c_sock_name.bv_val);
669 c->c_sock_name.bv_val = NULL;
671 c->c_sock_name.bv_len = 0;
673 c->c_sasl_bind_in_progress = 0;
674 if(c->c_sasl_bind_mech.bv_val != NULL) {
675 free(c->c_sasl_bind_mech.bv_val);
676 c->c_sasl_bind_mech.bv_val = NULL;
678 c->c_sasl_bind_mech.bv_len = 0;
680 slap_sasl_close( c );
682 if ( c->c_currentber != NULL ) {
683 ber_free( c->c_currentber, 1 );
684 c->c_currentber = NULL;
687 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
688 if ( sd != AC_SOCKET_INVALID ) {
689 slapd_remove( sd, 0 );
691 Statslog( LDAP_DEBUG_STATS,
692 "conn=%lu fd=%d closed\n",
693 connid, sd, 0, 0, 0 );
696 ber_sockbuf_free( c->c_sb );
698 c->c_sb = ber_sockbuf_alloc( );
701 ber_len_t max = sockbuf_max_incoming;
702 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
705 c->c_conn_state = SLAP_C_INVALID;
706 c->c_struct_state = SLAP_C_UNUSED;
709 int connection_state_closing( Connection *c )
711 /* c_mutex must be locked by caller */
715 assert( c->c_struct_state == SLAP_C_USED );
717 state = c->c_conn_state;
719 assert( state != SLAP_C_INVALID );
721 return state == SLAP_C_CLOSING;
724 static void connection_abandon( Connection *c )
726 /* c_mutex must be locked by caller */
730 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
734 /* remove pending operations */
735 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
736 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
737 LDAP_STAILQ_NEXT(o, o_next) = NULL;
742 void connection_closing( Connection *c )
744 assert( connections != NULL );
746 assert( c->c_struct_state == SLAP_C_USED );
747 assert( c->c_conn_state != SLAP_C_INVALID );
749 /* c_mutex must be locked by caller */
751 if( c->c_conn_state != SLAP_C_CLOSING ) {
754 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
756 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
757 "connection_closing: conn %lu readying socket %d for close.\n",
760 Debug( LDAP_DEBUG_TRACE,
761 "connection_closing: readying conn=%lu sd=%d for close\n",
762 c->c_connid, sd, 0 );
764 /* update state to closing */
765 c->c_conn_state = SLAP_C_CLOSING;
767 /* don't listen on this port anymore */
768 slapd_clr_read( sd, 1 );
770 /* abandon active operations */
771 connection_abandon( c );
773 /* wake write blocked operations */
774 slapd_clr_write( sd, 1 );
775 ldap_pvt_thread_cond_signal( &c->c_write_cv );
779 static void connection_close( Connection *c )
783 assert( connections != NULL );
785 assert( c->c_struct_state == SLAP_C_USED );
786 assert( c->c_conn_state == SLAP_C_CLOSING );
788 /* note: connections_mutex and c_mutex should be locked by caller */
790 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
791 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
793 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
794 "connection_close: conn %lu deferring sd %d\n",
797 Debug( LDAP_DEBUG_TRACE,
798 "connection_close: deferring conn=%lu sd=%d\n",
799 c->c_connid, sd, 0 );
805 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
806 "connection_close: conn %lu sd %d\n",
809 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
810 c->c_connid, sd, 0 );
812 connection_destroy( c );
815 unsigned long connections_nextid(void)
818 assert( connections != NULL );
820 ldap_pvt_thread_mutex_lock( &connections_mutex );
824 ldap_pvt_thread_mutex_unlock( &connections_mutex );
829 Connection* connection_first( ber_socket_t *index )
831 assert( connections != NULL );
832 assert( index != NULL );
834 ldap_pvt_thread_mutex_lock( &connections_mutex );
838 return connection_next(NULL, index);
841 Connection* connection_next( Connection *c, ber_socket_t *index )
843 assert( connections != NULL );
844 assert( index != NULL );
845 assert( *index <= dtblsize );
848 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
853 for(; *index < dtblsize; (*index)++) {
854 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
855 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
863 if( connections[*index].c_struct_state == SLAP_C_USED ) {
864 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
865 c = &connections[(*index)++];
869 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
870 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
874 ldap_pvt_thread_mutex_lock( &c->c_mutex );
880 void connection_done( Connection *c )
882 assert( connections != NULL );
885 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
888 ldap_pvt_thread_mutex_unlock( &connections_mutex );
892 * connection_activity - handle the request operation op on connection
893 * conn. This routine figures out what kind of operation it is and
894 * calls the appropriate stub to handle it.
898 #define INCR_OP(var,index) \
900 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
902 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
904 #else /* !SLAPD_MONITOR */
905 #define INCR_OP(var,index)
906 #endif /* !SLAPD_MONITOR */
909 connection_operation( void *arg_v )
912 struct co_arg *arg = arg_v;
913 ber_tag_t tag = arg->co_op->o_tag;
915 ber_tag_t oldtag = tag;
916 #endif /* SLAPD_MONITOR */
917 Connection *conn = arg->co_conn;
919 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
921 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
923 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
925 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
926 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
927 conn->c_connid, (long)tag ));
929 Debug( LDAP_DEBUG_ANY, "connection_operation: "
930 "error: SASL bind in progress (tag=%ld).\n",
933 send_ldap_result( conn, arg->co_op,
934 rc = LDAP_OPERATIONS_ERROR,
935 NULL, "SASL bind in progress", NULL, NULL );
936 goto operations_error;
941 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
942 rc = do_bind( conn, arg->co_op );
945 case LDAP_REQ_UNBIND:
946 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
947 rc = do_unbind( conn, arg->co_op );
951 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
952 rc = do_add( conn, arg->co_op );
955 case LDAP_REQ_DELETE:
956 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
957 rc = do_delete( conn, arg->co_op );
960 case LDAP_REQ_MODRDN:
961 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
962 rc = do_modrdn( conn, arg->co_op );
965 case LDAP_REQ_MODIFY:
966 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
967 rc = do_modify( conn, arg->co_op );
970 case LDAP_REQ_COMPARE:
971 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
972 rc = do_compare( conn, arg->co_op );
975 case LDAP_REQ_SEARCH:
976 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
977 rc = do_search( conn, arg->co_op );
980 case LDAP_REQ_ABANDON:
981 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
982 rc = do_abandon( conn, arg->co_op );
985 case LDAP_REQ_EXTENDED:
986 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
987 rc = do_extended( conn, arg->co_op );
992 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
993 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
994 conn->c_connid, tag ));
996 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
999 arg->co_op->o_tag = LBER_ERROR;
1000 send_ldap_disconnect( conn, arg->co_op,
1001 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
1006 #ifdef SLAPD_MONITOR
1008 #endif /* SLAPD_MONITOR */
1009 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1012 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1013 num_ops_completed++;
1014 #ifdef SLAPD_MONITOR
1017 num_ops_completed_[SLAP_OP_BIND]++;
1019 case LDAP_REQ_UNBIND:
1020 num_ops_completed_[SLAP_OP_UNBIND]++;
1023 num_ops_completed_[SLAP_OP_ADD]++;
1025 case LDAP_REQ_DELETE:
1026 num_ops_completed_[SLAP_OP_DELETE]++;
1028 case LDAP_REQ_MODRDN:
1029 num_ops_completed_[SLAP_OP_MODRDN]++;
1031 case LDAP_REQ_MODIFY:
1032 num_ops_completed_[SLAP_OP_MODIFY]++;
1034 case LDAP_REQ_COMPARE:
1035 num_ops_completed_[SLAP_OP_COMPARE]++;
1037 case LDAP_REQ_SEARCH:
1038 num_ops_completed_[SLAP_OP_SEARCH]++;
1040 case LDAP_REQ_ABANDON:
1041 num_ops_completed_[SLAP_OP_ABANDON]++;
1043 case LDAP_REQ_EXTENDED:
1044 num_ops_completed_[SLAP_OP_EXTENDED]++;
1047 #endif /* SLAPD_MONITOR */
1048 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1050 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1052 conn->c_n_ops_executing--;
1053 conn->c_n_ops_completed++;
1055 LDAP_STAILQ_REMOVE( &conn->c_ops, arg->co_op, slap_op, o_next);
1056 LDAP_STAILQ_NEXT(arg->co_op, o_next) = NULL;
1057 slap_op_free( arg->co_op );
1059 arg->co_conn = NULL;
1060 free( (char *) arg );
1065 case LDAP_REQ_UNBIND:
1066 /* c_mutex is locked */
1067 connection_closing( conn );
1071 conn->c_sasl_bind_in_progress =
1072 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1074 if( conn->c_conn_state == SLAP_C_BINDING) {
1075 conn->c_conn_state = SLAP_C_ACTIVE;
1079 connection_resched( conn );
1081 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1086 int connection_read(ber_socket_t s)
1090 assert( connections != NULL );
1092 ldap_pvt_thread_mutex_lock( &connections_mutex );
1094 /* get (locked) connection */
1095 c = connection_get( s );
1099 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1100 "connection_read: sock %ld no connection\n",
1103 Debug( LDAP_DEBUG_ANY,
1104 "connection_read(%ld): no connection!\n",
1109 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1115 if( c->c_conn_state == SLAP_C_CLOSING ) {
1117 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1118 "connection_read: conn %lu connection closing, ignoring input\n",
1121 Debug( LDAP_DEBUG_TRACE,
1122 "connection_read(%d): closing, ignoring input for id=%lu\n",
1123 s, c->c_connid, 0 );
1125 connection_return( c );
1126 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1131 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1132 "connection_read: conn %lu checking for input.\n", c->c_connid ));
1134 Debug( LDAP_DEBUG_TRACE,
1135 "connection_read(%d): checking for input on id=%lu\n",
1136 s, c->c_connid, 0 );
1140 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1141 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1143 #if 0 /* required by next #if 0 */
1149 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1150 "connection_read: conn %lu TLS accept error, error %d\n",
1153 Debug( LDAP_DEBUG_TRACE,
1154 "connection_read(%d): TLS accept error "
1155 "error=%d id=%lu, closing\n",
1156 s, rc, c->c_connid );
1158 c->c_needs_tls_accept = 0;
1159 /* connections_mutex and c_mutex are locked */
1160 connection_closing( c );
1163 /* Drain input before close, to allow SSL error codes
1164 * to propagate to client. */
1171 rc = select(s+1, &rfd, NULL, NULL, &tv);
1173 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
1177 connection_close( c );
1179 } else if ( rc == 0 ) {
1181 struct berval authid = { 0, NULL };
1183 c->c_needs_tls_accept = 0;
1185 /* we need to let SASL know */
1186 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1188 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1189 if( c->c_tls_ssf > c->c_ssf ) {
1190 c->c_ssf = c->c_tls_ssf;
1193 rc = dnX509peerNormalize( ssl, &authid );
1194 if ( rc != LDAP_SUCCESS ) {
1196 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1197 "connection_read: conn %lu unable to get TLS client DN, error %d\n",
1200 Debug( LDAP_DEBUG_TRACE,
1201 "connection_read(%d): unable to get TLS client DN "
1202 "error=%d id=%lu\n",
1203 s, rc, c->c_connid );
1206 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1207 if ( authid.bv_val ) free( authid.bv_val );
1209 connection_return( c );
1210 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1215 #ifdef HAVE_CYRUS_SASL
1216 if ( c->c_sasl_layers ) {
1217 c->c_sasl_layers = 0;
1219 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1221 if( rc != LDAP_SUCCESS ) {
1223 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1224 "connection_read: conn %lu SASL install error %d, closing\n",
1227 Debug( LDAP_DEBUG_TRACE,
1228 "connection_read(%d): SASL install error "
1229 "error=%d id=%lu, closing\n",
1230 s, rc, c->c_connid );
1232 /* connections_mutex and c_mutex are locked */
1233 connection_closing( c );
1234 connection_close( c );
1235 connection_return( c );
1236 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1242 #define CONNECTION_INPUT_LOOP 1
1244 #ifdef DATA_READY_LOOP
1245 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1246 #elif CONNECTION_INPUT_LOOP
1250 /* How do we do this without getting into a busy loop ? */
1251 rc = connection_input( c );
1256 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1257 "connection_read: conn %lu input error %d, closing.\n",
1260 Debug( LDAP_DEBUG_TRACE,
1261 "connection_read(%d): input error=%d id=%lu, closing.\n",
1262 s, rc, c->c_connid );
1264 /* connections_mutex and c_mutex are locked */
1265 connection_closing( c );
1266 connection_close( c );
1267 connection_return( c );
1268 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1272 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1273 slapd_set_read( s, 1 );
1276 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1277 slapd_set_write( s, 1 );
1280 connection_return( c );
1281 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1295 #ifdef LDAP_CONNECTIONLESS
1300 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1303 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1304 "connection_input: conn %lu ber_alloc failed.\n",
1307 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1314 #ifdef LDAP_CONNECTIONLESS
1317 char peername[sizeof("IP=255.255.255.255:65336")];
1318 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1319 sizeof(struct sockaddr));
1320 if (len != sizeof(struct sockaddr))
1322 sprintf( peername, "IP=%s:%d",
1323 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1324 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1325 Statslog( LDAP_DEBUG_STATS,
1326 "conn=%lu UDP request from %s (%s) accepted.\n",
1327 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1330 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1331 if ( tag != LDAP_TAG_MESSAGE ) {
1335 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1338 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1339 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1340 conn->c_connid, err, sock_errstr(err) ));
1342 Debug( LDAP_DEBUG_TRACE,
1343 "ber_get_next on fd %d failed errno=%d (%s)\n",
1344 sd, err, sock_errstr(err) );
1346 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1347 /* log, close and send error */
1348 ber_free( conn->c_currentber, 1 );
1349 conn->c_currentber = NULL;
1356 ber = conn->c_currentber;
1357 conn->c_currentber = NULL;
1359 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1360 /* log, close and send error */
1362 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1363 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1364 conn->c_connid, tag ));
1366 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1373 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1374 /* log, close and send error */
1376 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1377 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1378 conn->c_connid, tag ));
1380 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1388 #ifdef LDAP_CONNECTIONLESS
1389 if (conn->c_is_udp) {
1390 if (tag == LBER_OCTETSTRING) {
1391 ber_get_stringa( ber, &cdn );
1392 tag = ber_peek_tag(ber, &len);
1394 if (tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH) {
1396 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1397 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1398 conn->c_connid, tag ));
1400 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1408 if(tag == LDAP_REQ_BIND) {
1409 /* immediately abandon all exiting operations upon BIND */
1410 connection_abandon( conn );
1413 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1415 op->vrFilter = NULL;
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 );