2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2015 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
33 #include <ac/socket.h>
35 #include <ac/string.h>
37 #include <ac/unistd.h>
42 #ifdef LDAP_CONNECTIONLESS
43 #include "../../libraries/liblber/lber-int.h" /* ber_int_sb_read() */
47 #include "slapi/slapi.h"
50 /* protected by connections_mutex */
51 static ldap_pvt_thread_mutex_t connections_mutex;
52 static Connection *connections = NULL;
54 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
55 static unsigned long conn_nextid = SLAPD_SYNC_SYNCCONN_OFFSET;
57 static const char conn_lost_str[] = "connection lost";
60 connection_state2str( int state )
63 case SLAP_C_INVALID: return "!";
64 case SLAP_C_INACTIVE: return "|";
65 case SLAP_C_CLOSING: return "C";
66 case SLAP_C_ACTIVE: return "";
67 case SLAP_C_BINDING: return "B";
68 case SLAP_C_CLIENT: return "L";
74 static Connection* connection_get( ber_socket_t s );
76 typedef struct conn_readinfo {
78 ldap_pvt_thread_start_t *func;
84 static int connection_input( Connection *c, conn_readinfo *cri );
85 static void connection_close( Connection *c );
87 static int connection_op_activate( Operation *op );
88 static void connection_op_queue( Operation *op );
89 static int connection_resched( Connection *conn );
90 static void connection_abandon( Connection *conn );
91 static void connection_destroy( Connection *c );
93 static ldap_pvt_thread_start_t connection_operation;
96 * Initialize connection management infrastructure.
98 int connections_init(void)
102 assert( connections == NULL );
104 if( connections != NULL) {
105 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
110 /* should check return of every call */
111 ldap_pvt_thread_mutex_init( &connections_mutex );
112 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
114 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
116 if( connections == NULL ) {
117 Debug( LDAP_DEBUG_ANY, "connections_init: "
118 "allocation (%d*%ld) of connection array failed\n",
119 dtblsize, (long) sizeof(Connection), 0 );
123 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
124 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
126 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
129 * per entry initialization of the Connection array initialization
130 * will be done by connection_init()
137 * Destroy connection management infrastructure.
140 int connections_destroy(void)
144 /* should check return of every call */
146 if( connections == NULL) {
147 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
152 for ( i = 0; i < dtblsize; i++ ) {
153 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
154 ber_sockbuf_free( connections[i].c_sb );
155 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
156 ldap_pvt_thread_mutex_destroy( &connections[i].c_write1_mutex );
157 ldap_pvt_thread_mutex_destroy( &connections[i].c_write2_mutex );
158 ldap_pvt_thread_cond_destroy( &connections[i].c_write1_cv );
159 ldap_pvt_thread_cond_destroy( &connections[i].c_write2_cv );
161 if ( slapi_plugins_used ) {
162 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
172 ldap_pvt_thread_mutex_destroy( &connections_mutex );
173 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
178 * shutdown all connections
180 int connections_shutdown(void)
184 for ( i = 0; i < dtblsize; i++ ) {
185 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
186 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
187 if( connections[i].c_struct_state == SLAP_C_USED ) {
189 /* give persistent clients a chance to cleanup */
190 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
191 ldap_pvt_thread_pool_submit( &connection_pool,
192 connections[i].c_clientfunc, connections[i].c_clientarg );
194 /* c_mutex is locked */
195 connection_closing( &connections[i], "slapd shutdown" );
196 connection_close( &connections[i] );
199 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
207 * Timeout idle connections.
209 int connections_timeout_idle(time_t now)
211 int i = 0, writers = 0;
212 ber_socket_t connindex;
216 old = slapd_get_writetime();
218 for( c = connection_first( &connindex );
220 c = connection_next( c, &connindex ) )
222 /* Don't timeout a slow-running request or a persistent
223 * outbound connection. But if it has a writewaiter, see
224 * if the waiter has been there too long.
226 if(( c->c_n_ops_executing && !c->c_writewaiter)
227 || c->c_conn_state == SLAP_C_CLIENT ) {
231 if( global_idletimeout &&
232 difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
234 connection_closing( c, "idletimeout" );
235 connection_close( c );
239 if ( c->c_writewaiter && global_writetimeout ) {
241 if( difftime( c->c_activitytime+global_writetimeout, now) < 0 ) {
243 connection_closing( c, "writetimeout" );
244 connection_close( c );
250 connection_done( c );
251 if ( old && !writers )
252 slapd_clr_writetime( old );
257 /* Drop all client connections */
258 void connections_drop()
261 ber_socket_t connindex;
263 for( c = connection_first( &connindex );
265 c = connection_next( c, &connindex ) )
267 /* Don't close a slow-running request or a persistent
268 * outbound connection.
270 if(( c->c_n_ops_executing && !c->c_writewaiter)
271 || c->c_conn_state == SLAP_C_CLIENT ) {
274 connection_closing( c, "dropping" );
275 connection_close( c );
277 connection_done( c );
280 static Connection* connection_get( ber_socket_t s )
284 Debug( LDAP_DEBUG_ARGS,
285 "connection_get(%ld)\n",
288 assert( connections != NULL );
290 if(s == AC_SOCKET_INVALID) return NULL;
292 assert( s < dtblsize );
296 ldap_pvt_thread_mutex_lock( &c->c_mutex );
298 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
300 if( c->c_struct_state != SLAP_C_USED ) {
301 /* connection must have been closed due to resched */
303 Debug( LDAP_DEBUG_CONNS,
304 "connection_get(%d): connection not used\n",
306 assert( c->c_conn_state == SLAP_C_INVALID );
307 assert( c->c_sd == AC_SOCKET_INVALID );
309 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
313 Debug( LDAP_DEBUG_TRACE,
314 "connection_get(%d): got connid=%lu\n",
319 assert( c->c_struct_state == SLAP_C_USED );
320 assert( c->c_conn_state != SLAP_C_INVALID );
321 assert( c->c_sd != AC_SOCKET_INVALID );
323 #ifndef SLAPD_MONITOR
324 if ( global_idletimeout > 0 )
325 #endif /* ! SLAPD_MONITOR */
327 c->c_activitytime = slap_get_time();
334 static void connection_return( Connection *c )
336 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
339 Connection * connection_init(
343 const char* peername,
346 struct berval *authid
347 LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
352 ber_socket_t sfd = SLAP_FD2SOCK(s);
354 assert( connections != NULL );
356 assert( listener != NULL );
357 assert( dnsname != NULL );
358 assert( peername != NULL );
361 assert( !( flags & CONN_IS_TLS ));
364 if( s == AC_SOCKET_INVALID ) {
365 Debug( LDAP_DEBUG_ANY,
366 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
371 assert( s < dtblsize );
373 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
376 assert( c->c_struct_state == SLAP_C_UNUSED );
380 c->c_send_ldap_result = slap_send_ldap_result;
381 c->c_send_search_entry = slap_send_search_entry;
382 c->c_send_search_reference = slap_send_search_reference;
383 c->c_send_ldap_extended = slap_send_ldap_extended;
384 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
386 BER_BVZERO( &c->c_authmech );
387 BER_BVZERO( &c->c_dn );
388 BER_BVZERO( &c->c_ndn );
390 c->c_listener = NULL;
391 BER_BVZERO( &c->c_peer_domain );
392 BER_BVZERO( &c->c_peer_name );
394 LDAP_STAILQ_INIT(&c->c_ops);
395 LDAP_STAILQ_INIT(&c->c_pending_ops);
398 c->c_txn = CONN_TXN_INACTIVE;
399 c->c_txn_backend = NULL;
400 LDAP_STAILQ_INIT(&c->c_txn_ops);
403 BER_BVZERO( &c->c_sasl_bind_mech );
405 c->c_sasl_authctx = NULL;
406 c->c_sasl_sockctx = NULL;
407 c->c_sasl_extra = NULL;
408 c->c_sasl_bindop = NULL;
410 c->c_sb = ber_sockbuf_alloc( );
413 ber_len_t max = sockbuf_max_incoming;
414 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
417 c->c_currentber = NULL;
419 /* should check status of thread calls */
420 ldap_pvt_thread_mutex_init( &c->c_mutex );
421 ldap_pvt_thread_mutex_init( &c->c_write1_mutex );
422 ldap_pvt_thread_mutex_init( &c->c_write2_mutex );
423 ldap_pvt_thread_cond_init( &c->c_write1_cv );
424 ldap_pvt_thread_cond_init( &c->c_write2_cv );
427 if ( slapi_plugins_used ) {
428 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
433 ldap_pvt_thread_mutex_lock( &c->c_mutex );
435 assert( BER_BVISNULL( &c->c_authmech ) );
436 assert( BER_BVISNULL( &c->c_dn ) );
437 assert( BER_BVISNULL( &c->c_ndn ) );
438 assert( c->c_listener == NULL );
439 assert( BER_BVISNULL( &c->c_peer_domain ) );
440 assert( BER_BVISNULL( &c->c_peer_name ) );
441 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
442 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
444 assert( c->c_txn == CONN_TXN_INACTIVE );
445 assert( c->c_txn_backend == NULL );
446 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
448 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
449 assert( c->c_sasl_done == 0 );
450 assert( c->c_sasl_authctx == NULL );
451 assert( c->c_sasl_sockctx == NULL );
452 assert( c->c_sasl_extra == NULL );
453 assert( c->c_sasl_bindop == NULL );
454 assert( c->c_currentber == NULL );
455 assert( c->c_writewaiter == 0);
456 assert( c->c_writers == 0);
458 c->c_listener = listener;
461 if ( flags & CONN_IS_CLIENT ) {
463 ldap_pvt_thread_mutex_lock( &connections_mutex );
464 c->c_conn_state = SLAP_C_CLIENT;
465 c->c_struct_state = SLAP_C_USED;
466 ldap_pvt_thread_mutex_unlock( &connections_mutex );
467 c->c_close_reason = "?"; /* should never be needed */
468 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
469 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
474 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
475 ber_str2bv( peername, 0, 1, &c->c_peer_name );
477 c->c_n_ops_received = 0;
478 c->c_n_ops_executing = 0;
479 c->c_n_ops_pending = 0;
480 c->c_n_ops_completed = 0;
486 /* set to zero until bind, implies LDAP_VERSION3 */
489 #ifndef SLAPD_MONITOR
490 if ( global_idletimeout > 0 )
491 #endif /* ! SLAPD_MONITOR */
493 c->c_activitytime = c->c_starttime = slap_get_time();
496 #ifdef LDAP_CONNECTIONLESS
498 if( flags & CONN_IS_UDP ) {
501 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
502 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
504 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
505 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
506 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
507 LBER_SBIOD_LEVEL_PROVIDER, NULL );
509 #endif /* LDAP_CONNECTIONLESS */
511 if ( flags & CONN_IS_IPC ) {
513 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
514 LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
516 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
517 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
518 #ifdef LDAP_PF_LOCAL_SENDMSG
519 if ( !BER_BVISEMPTY( peerbv ))
520 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
523 #endif /* LDAP_PF_LOCAL */
526 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
527 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
529 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
530 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
534 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
535 INT_MAX, (void*)"ldap_" );
538 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
539 c /* non-NULL */ ) < 0 )
541 Debug( LDAP_DEBUG_ANY,
542 "connection_init(%d, %s): set nonblocking failed\n",
543 s, c->c_peer_name.bv_val, 0 );
546 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
547 id = c->c_connid = conn_nextid++;
548 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
550 ldap_pvt_thread_mutex_lock( &connections_mutex );
551 c->c_conn_state = SLAP_C_INACTIVE;
552 c->c_struct_state = SLAP_C_USED;
553 ldap_pvt_thread_mutex_unlock( &connections_mutex );
554 c->c_close_reason = "?"; /* should never be needed */
556 c->c_ssf = c->c_transport_ssf = ssf;
560 if ( flags & CONN_IS_TLS ) {
562 c->c_needs_tls_accept = 1;
565 c->c_needs_tls_accept = 0;
569 slap_sasl_open( c, 0 );
570 slap_sasl_external( c, ssf, authid );
572 slapd_add_internal( s, 1 );
574 backend_connection_init(c);
575 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
577 if ( !(flags & CONN_IS_UDP ))
578 Statslog( LDAP_DEBUG_STATS,
579 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
580 id, (long) s, peername, listener->sl_name.bv_val, 0 );
585 void connection2anonymous( Connection *c )
587 assert( connections != NULL );
591 ber_len_t max = sockbuf_max_incoming;
592 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
595 if ( !BER_BVISNULL( &c->c_authmech ) ) {
596 ch_free(c->c_authmech.bv_val);
598 BER_BVZERO( &c->c_authmech );
600 if ( !BER_BVISNULL( &c->c_dn ) ) {
601 ch_free(c->c_dn.bv_val);
603 BER_BVZERO( &c->c_dn );
605 if ( !BER_BVISNULL( &c->c_ndn ) ) {
606 ch_free(c->c_ndn.bv_val);
608 BER_BVZERO( &c->c_ndn );
610 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
611 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
613 BER_BVZERO( &c->c_sasl_authz_dn );
615 c->c_authz_backend = NULL;
619 connection_destroy( Connection *c )
621 unsigned long connid;
622 const char *close_reason;
626 assert( connections != NULL );
628 assert( c->c_struct_state != SLAP_C_UNUSED );
629 assert( c->c_conn_state != SLAP_C_INVALID );
630 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
631 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
633 assert( c->c_txn == CONN_TXN_INACTIVE );
634 assert( c->c_txn_backend == NULL );
635 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
637 assert( c->c_writewaiter == 0);
638 assert( c->c_writers == 0);
640 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
641 connid = c->c_connid;
642 close_reason = c->c_close_reason;
644 ldap_pvt_thread_mutex_lock( &connections_mutex );
645 c->c_struct_state = SLAP_C_PENDING;
646 ldap_pvt_thread_mutex_unlock( &connections_mutex );
648 backend_connection_destroy(c);
653 c->c_activitytime = c->c_starttime = 0;
655 connection2anonymous( c );
656 c->c_listener = NULL;
658 if(c->c_peer_domain.bv_val != NULL) {
659 free(c->c_peer_domain.bv_val);
661 BER_BVZERO( &c->c_peer_domain );
662 if(c->c_peer_name.bv_val != NULL) {
663 free(c->c_peer_name.bv_val);
665 BER_BVZERO( &c->c_peer_name );
667 c->c_sasl_bind_in_progress = 0;
668 if(c->c_sasl_bind_mech.bv_val != NULL) {
669 free(c->c_sasl_bind_mech.bv_val);
671 BER_BVZERO( &c->c_sasl_bind_mech );
673 slap_sasl_close( c );
675 if ( c->c_currentber != NULL ) {
676 ber_free( c->c_currentber, 1 );
677 c->c_currentber = NULL;
682 /* call destructors, then constructors; avoids unnecessary allocation */
683 if ( slapi_plugins_used ) {
684 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
689 c->c_sd = AC_SOCKET_INVALID;
690 c->c_conn_state = SLAP_C_INVALID;
691 c->c_struct_state = SLAP_C_UNUSED;
692 c->c_close_reason = "?"; /* should never be needed */
695 c->c_sb = ber_sockbuf_alloc( );
697 ber_len_t max = sockbuf_max_incoming;
698 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
701 /* c must be fully reset by this point; when we call slapd_remove
702 * it may get immediately reused by a new connection.
704 if ( sd != AC_SOCKET_INVALID ) {
705 slapd_remove( sd, sb, 1, 0, 0 );
707 if ( close_reason == NULL ) {
708 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
709 connid, (long) sd, 0, 0, 0 );
711 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
712 connid, (long) sd, close_reason, 0, 0 );
717 int connection_valid( Connection *c )
719 /* c_mutex must be locked by caller */
723 return c->c_struct_state == SLAP_C_USED &&
724 c->c_conn_state >= SLAP_C_ACTIVE &&
725 c->c_conn_state <= SLAP_C_CLIENT;
728 static void connection_abandon( Connection *c )
730 /* c_mutex must be locked by caller */
732 Operation *o, *next, op = {0};
737 op.o_connid = c->c_connid;
738 op.o_tag = LDAP_REQ_ABANDON;
740 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
741 SlapReply rs = {REP_RESULT};
743 next = LDAP_STAILQ_NEXT( o, o_next );
744 /* don't abandon an op twice */
747 op.orn_msgid = o->o_msgid;
749 op.o_bd = frontendDB;
750 frontendDB->be_abandon( &op, &rs );
754 /* remove operations in pending transaction */
755 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
756 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
757 LDAP_STAILQ_NEXT(o, o_next) = NULL;
758 slap_op_free( o, NULL );
761 /* clear transaction */
762 c->c_txn_backend = NULL;
763 c->c_txn = CONN_TXN_INACTIVE;
766 /* remove pending operations */
767 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
768 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
769 LDAP_STAILQ_NEXT(o, o_next) = NULL;
770 slap_op_free( o, NULL );
775 connection_wake_writers( Connection *c )
777 /* wake write blocked operations */
778 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
779 if ( c->c_writers > 0 ) {
780 c->c_writers = -c->c_writers;
781 ldap_pvt_thread_cond_broadcast( &c->c_write1_cv );
782 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
783 if ( c->c_writewaiter ) {
784 ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
785 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
786 slapd_clr_write( c->c_sd, 1 );
787 ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
789 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
790 while ( c->c_writers ) {
791 ldap_pvt_thread_cond_wait( &c->c_write1_cv, &c->c_write1_mutex );
793 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
795 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
796 slapd_clr_write( c->c_sd, 1 );
800 void connection_closing( Connection *c, const char *why )
802 assert( connections != NULL );
805 if ( c->c_struct_state != SLAP_C_USED ) return;
807 assert( c->c_conn_state != SLAP_C_INVALID );
809 /* c_mutex must be locked by caller */
811 if( c->c_conn_state != SLAP_C_CLOSING ) {
812 Debug( LDAP_DEBUG_CONNS,
813 "connection_closing: readying conn=%lu sd=%d for close\n",
814 c->c_connid, c->c_sd, 0 );
815 /* update state to closing */
816 c->c_conn_state = SLAP_C_CLOSING;
817 c->c_close_reason = why;
819 /* don't listen on this port anymore */
820 slapd_clr_read( c->c_sd, 0 );
822 /* abandon active operations */
823 connection_abandon( c );
825 /* wake write blocked operations */
826 connection_wake_writers( c );
828 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
829 /* Client closed connection after doing Unbind. */
830 c->c_close_reason = NULL;
835 connection_close( Connection *c )
837 assert( connections != NULL );
840 if ( c->c_struct_state != SLAP_C_USED ) return;
842 assert( c->c_conn_state == SLAP_C_CLOSING );
844 /* NOTE: c_mutex should be locked by caller */
846 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
847 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
849 Debug( LDAP_DEBUG_CONNS,
850 "connection_close: deferring conn=%lu sd=%d\n",
851 c->c_connid, c->c_sd, 0 );
855 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
856 c->c_connid, c->c_sd, 0 );
858 connection_destroy( c );
861 unsigned long connections_nextid(void)
864 assert( connections != NULL );
866 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
870 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
876 * Loop through the connections:
878 * for (c = connection_first(&i); c; c = connection_next(c, &i)) ...;
879 * connection_done(c);
881 * 'i' is the cursor, initialized by connection_first().
882 * 'c_mutex' is locked in the returned connection. The functions must
883 * be passed the previous return value so they can unlock it again.
886 Connection* connection_first( ber_socket_t *index )
888 assert( connections != NULL );
889 assert( index != NULL );
891 ldap_pvt_thread_mutex_lock( &connections_mutex );
892 for( *index = 0; *index < dtblsize; (*index)++) {
893 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
897 ldap_pvt_thread_mutex_unlock( &connections_mutex );
899 return connection_next(NULL, index);
902 /* Next connection in loop, see connection_first() */
903 Connection* connection_next( Connection *c, ber_socket_t *index )
905 assert( connections != NULL );
906 assert( index != NULL );
907 assert( *index <= dtblsize );
909 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
913 ldap_pvt_thread_mutex_lock( &connections_mutex );
914 for(; *index < dtblsize; (*index)++) {
916 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
917 /* FIXME: accessing c_conn_state without locking c_mutex */
918 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
922 if( connections[*index].c_struct_state == SLAP_C_USED ) {
923 c = &connections[(*index)++];
924 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
926 ldap_pvt_thread_mutex_unlock( &connections_mutex );
927 ldap_pvt_thread_mutex_lock( &c->c_mutex );
928 ldap_pvt_thread_mutex_lock( &connections_mutex );
929 if ( c->c_struct_state != SLAP_C_USED ) {
930 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
935 assert( c->c_conn_state != SLAP_C_INVALID );
939 c_struct = connections[*index].c_struct_state;
940 if ( c_struct == SLAP_C_PENDING )
942 assert( c_struct == SLAP_C_UNUSED );
943 /* FIXME: accessing c_conn_state without locking c_mutex */
944 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
947 ldap_pvt_thread_mutex_unlock( &connections_mutex );
951 /* End connection loop, see connection_first() */
952 void connection_done( Connection *c )
954 assert( connections != NULL );
956 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
960 * connection_activity - handle the request operation op on connection
961 * conn. This routine figures out what kind of operation it is and
962 * calls the appropriate stub to handle it.
966 /* FIXME: returns 0 in case of failure */
967 #define INCR_OP_INITIATED(index) \
969 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
970 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated_[(index)], 1); \
971 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
973 #define INCR_OP_COMPLETED(index) \
975 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
976 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
977 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed_[(index)], 1); \
978 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
980 #else /* !SLAPD_MONITOR */
981 #define INCR_OP_INITIATED(index) do { } while (0)
982 #define INCR_OP_COMPLETED(index) \
984 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
985 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
986 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
988 #endif /* !SLAPD_MONITOR */
991 * NOTE: keep in sync with enum in slapd.h
993 static BI_op_func *opfun[] = {
1007 /* Counters are per-thread, not per-connection.
1010 conn_counter_destroy( void *key, void *data )
1012 slap_counters_t **prev, *sc;
1014 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
1015 for ( prev = &slap_counters.sc_next, sc = slap_counters.sc_next; sc;
1016 prev = &sc->sc_next, sc = sc->sc_next ) {
1020 *prev = sc->sc_next;
1021 /* Copy data to main counter */
1022 ldap_pvt_mp_add( slap_counters.sc_bytes, sc->sc_bytes );
1023 ldap_pvt_mp_add( slap_counters.sc_pdu, sc->sc_pdu );
1024 ldap_pvt_mp_add( slap_counters.sc_entries, sc->sc_entries );
1025 ldap_pvt_mp_add( slap_counters.sc_refs, sc->sc_refs );
1026 ldap_pvt_mp_add( slap_counters.sc_ops_initiated, sc->sc_ops_initiated );
1027 ldap_pvt_mp_add( slap_counters.sc_ops_completed, sc->sc_ops_completed );
1028 #ifdef SLAPD_MONITOR
1029 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
1030 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_initiated_[ i ] );
1031 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_completed_[ i ] );
1033 #endif /* SLAPD_MONITOR */
1034 slap_counters_destroy( sc );
1035 ber_memfree_x( data, NULL );
1039 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1043 conn_counter_init( Operation *op, void *ctx )
1045 slap_counters_t *sc;
1048 if ( ldap_pvt_thread_pool_getkey(
1049 ctx, (void *)conn_counter_init, &vsc, NULL ) || !vsc ) {
1050 vsc = ch_malloc( sizeof( slap_counters_t ));
1052 slap_counters_init( sc );
1053 ldap_pvt_thread_pool_setkey( ctx, (void*)conn_counter_init, vsc,
1054 conn_counter_destroy, NULL, NULL );
1056 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
1057 sc->sc_next = slap_counters.sc_next;
1058 slap_counters.sc_next = sc;
1059 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1061 op->o_counters = vsc;
1065 connection_operation( void *ctx, void *arg_v )
1067 int rc = LDAP_OTHER, cancel;
1068 Operation *op = arg_v;
1069 SlapReply rs = {REP_RESULT};
1070 ber_tag_t tag = op->o_tag;
1071 slap_op_t opidx = SLAP_OP_LAST;
1072 Connection *conn = op->o_conn;
1073 void *memctx = NULL;
1074 void *memctx_null = NULL;
1077 conn_counter_init( op, ctx );
1078 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1079 /* FIXME: returns 0 in case of failure */
1080 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated, 1);
1081 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1083 op->o_threadctx = ctx;
1084 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1088 case LDAP_REQ_UNBIND:
1090 case LDAP_REQ_DELETE:
1091 case LDAP_REQ_MODDN:
1092 case LDAP_REQ_MODIFY:
1093 case LDAP_REQ_COMPARE:
1094 case LDAP_REQ_SEARCH:
1095 case LDAP_REQ_ABANDON:
1096 case LDAP_REQ_EXTENDED:
1099 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1100 "conn %lu unknown LDAP request 0x%lx\n",
1101 conn->c_connid, tag, 0 );
1102 op->o_tag = LBER_ERROR;
1103 rs.sr_err = LDAP_PROTOCOL_ERROR;
1104 rs.sr_text = "unknown LDAP request";
1105 send_ldap_disconnect( op, &rs );
1106 rc = SLAPD_DISCONNECT;
1107 goto operations_error;
1110 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1111 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1112 "error: SASL bind in progress (tag=%ld).\n",
1114 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1115 "SASL bind in progress" );
1116 rc = LDAP_OPERATIONS_ERROR;
1117 goto operations_error;
1121 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1122 ( tag == LDAP_REQ_ADD ) ||
1123 ( tag == LDAP_REQ_DELETE ) ||
1124 ( tag == LDAP_REQ_MODIFY ) ||
1125 ( tag == LDAP_REQ_MODRDN )))
1127 /* Disable SLAB allocator for all update operations
1128 issued inside of a transaction */
1129 op->o_tmpmemctx = NULL;
1130 op->o_tmpmfuncs = &ch_mfuncs;
1134 /* We can use Thread-Local storage for most mallocs. We can
1135 * also use TL for ber parsing, but not on Add or Modify.
1138 memsiz = ber_len( op->o_ber ) * 64;
1139 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1141 memsiz = SLAP_SLAB_SIZE;
1143 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1144 op->o_tmpmemctx = memctx;
1145 op->o_tmpmfuncs = &slap_sl_mfuncs;
1146 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1147 /* Note - the ber and its buffer are already allocated from
1148 * regular memory; this only affects subsequent mallocs that
1149 * ber_scanf may invoke.
1151 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1155 opidx = slap_req2op( tag );
1156 assert( opidx != SLAP_OP_LAST );
1157 INCR_OP_INITIATED( opidx );
1158 rc = (*(opfun[opidx]))( op, &rs );
1161 if ( rc == SLAPD_DISCONNECT ) {
1164 } else if ( opidx != SLAP_OP_LAST ) {
1165 /* increment completed operations count
1166 * only if operation was initiated
1167 * and rc != SLAPD_DISCONNECT */
1168 INCR_OP_COMPLETED( opidx );
1171 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1173 if ( opidx == SLAP_OP_BIND && conn->c_conn_state == SLAP_C_BINDING )
1174 conn->c_conn_state = SLAP_C_ACTIVE;
1176 cancel = op->o_cancel;
1177 if ( cancel != SLAP_CANCEL_NONE && cancel != SLAP_CANCEL_DONE ) {
1178 if ( cancel == SLAP_CANCEL_REQ ) {
1179 op->o_cancel = rc == SLAPD_ABANDON
1180 ? SLAP_CANCEL_ACK : LDAP_TOO_LATE;
1184 /* Fake a cond_wait with thread_yield, then
1185 * verify the result properly mutex-protected.
1187 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1189 ldap_pvt_thread_yield();
1190 } while ( (cancel = op->o_cancel) != SLAP_CANCEL_NONE
1191 && cancel != SLAP_CANCEL_DONE );
1192 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1193 } while ( (cancel = op->o_cancel) != SLAP_CANCEL_NONE
1194 && cancel != SLAP_CANCEL_DONE );
1197 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1199 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1200 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1201 conn->c_n_ops_executing--;
1202 conn->c_n_ops_completed++;
1206 case LDAP_REQ_UNBIND:
1207 /* c_mutex is locked */
1208 connection_closing( conn,
1209 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1213 connection_resched( conn );
1214 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1215 slap_op_free( op, ctx );
1219 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1221 Connection *connection_client_setup(
1223 ldap_pvt_thread_start_t *func,
1227 ber_socket_t sfd = SLAP_SOCKNEW( s );
1229 c = connection_init( sfd, (Listener *)&dummy_list, "", "",
1230 CONN_IS_CLIENT, 0, NULL
1231 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
1233 c->c_clientfunc = func;
1234 c->c_clientarg = arg;
1236 slapd_add_internal( sfd, 0 );
1241 void connection_client_enable(
1244 slapd_set_read( c->c_sd, 1 );
1247 void connection_client_stop(
1251 ber_socket_t s = c->c_sd;
1253 /* get (locked) connection */
1254 c = connection_get( s );
1256 assert( c->c_conn_state == SLAP_C_CLIENT );
1258 c->c_listener = NULL;
1259 c->c_conn_state = SLAP_C_INVALID;
1260 c->c_struct_state = SLAP_C_UNUSED;
1261 c->c_sd = AC_SOCKET_INVALID;
1262 c->c_close_reason = "?"; /* should never be needed */
1264 c->c_sb = ber_sockbuf_alloc( );
1266 ber_len_t max = sockbuf_max_incoming;
1267 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1269 slapd_remove( s, sb, 0, 1, 0 );
1271 connection_return( c );
1274 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1276 static void* connection_read_thread( void* ctx, void* argv )
1279 conn_readinfo cri = { NULL, NULL, NULL, NULL, 0 };
1280 ber_socket_t s = (long)argv;
1283 * read incoming LDAP requests. If there is more than one,
1284 * the first one is returned with new_op
1287 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1288 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1289 return (void*)(long)rc;
1292 /* execute a single queued request in the same thread */
1293 if( cri.op && !cri.nullop ) {
1294 rc = (long)connection_operation( ctx, cri.op );
1295 } else if ( cri.func ) {
1296 rc = (long)cri.func( ctx, cri.arg );
1299 return (void*)(long)rc;
1302 int connection_read_activate( ber_socket_t s )
1307 * suspend reading on this file descriptor until a connection processing
1308 * thread reads data on it. Otherwise the listener thread will repeatedly
1309 * submit the same event on it to the pool.
1311 rc = slapd_clr_read( s, 0 );
1315 /* Don't let blocked writers block a pause request */
1316 if ( connections[s].c_writewaiter &&
1317 ldap_pvt_thread_pool_pausing( &connection_pool ))
1318 connection_wake_writers( &connections[s] );
1320 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1321 connection_read_thread, (void *)(long)s );
1324 Debug( LDAP_DEBUG_ANY,
1325 "connection_read_activate(%d): submit failed (%d)\n",
1333 connection_read( ber_socket_t s, conn_readinfo *cri )
1338 assert( connections != NULL );
1340 /* get (locked) connection */
1341 c = connection_get( s );
1344 Debug( LDAP_DEBUG_ANY,
1345 "connection_read(%ld): no connection!\n",
1353 if( c->c_conn_state == SLAP_C_CLOSING ) {
1354 Debug( LDAP_DEBUG_CONNS,
1355 "connection_read(%d): closing, ignoring input for id=%lu\n",
1356 s, c->c_connid, 0 );
1357 connection_return( c );
1361 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1362 cri->func = c->c_clientfunc;
1363 cri->arg = c->c_clientarg;
1364 /* read should already be cleared */
1365 connection_return( c );
1369 Debug( LDAP_DEBUG_TRACE,
1370 "connection_read(%d): checking for input on id=%lu\n",
1371 s, c->c_connid, 0 );
1374 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1375 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1377 Debug( LDAP_DEBUG_TRACE,
1378 "connection_read(%d): TLS accept failure "
1379 "error=%d id=%lu, closing\n",
1380 s, rc, c->c_connid );
1382 c->c_needs_tls_accept = 0;
1383 /* c_mutex is locked */
1384 connection_closing( c, "TLS negotiation failure" );
1385 connection_close( c );
1386 connection_return( c );
1389 } else if ( rc == 0 ) {
1391 struct berval authid = BER_BVNULL;
1393 c->c_needs_tls_accept = 0;
1395 /* we need to let SASL know */
1396 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1398 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1399 if( c->c_tls_ssf > c->c_ssf ) {
1400 c->c_ssf = c->c_tls_ssf;
1403 rc = dnX509peerNormalize( ssl, &authid );
1404 if ( rc != LDAP_SUCCESS ) {
1405 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1406 "unable to get TLS client DN, error=%d id=%lu\n",
1407 s, rc, c->c_connid );
1409 Statslog( LDAP_DEBUG_STATS,
1410 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1411 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1412 slap_sasl_external( c, c->c_tls_ssf, &authid );
1413 if ( authid.bv_val ) free( authid.bv_val );
1414 } else if ( rc == 1 && ber_sockbuf_ctrl( c->c_sb,
1415 LBER_SB_OPT_NEEDS_WRITE, NULL )) { /* need to retry */
1416 slapd_set_write( s, 1 );
1417 connection_return( c );
1421 /* if success and data is ready, fall thru to data input loop */
1422 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1424 slapd_set_read( s, 1 );
1425 connection_return( c );
1431 #ifdef HAVE_CYRUS_SASL
1432 if ( c->c_sasl_layers ) {
1433 /* If previous layer is not removed yet, give up for now */
1434 if ( !c->c_sasl_sockctx ) {
1435 slapd_set_read( s, 1 );
1436 connection_return( c );
1440 c->c_sasl_layers = 0;
1442 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1443 if( rc != LDAP_SUCCESS ) {
1444 Debug( LDAP_DEBUG_TRACE,
1445 "connection_read(%d): SASL install error "
1446 "error=%d id=%lu, closing\n",
1447 s, rc, c->c_connid );
1449 /* c_mutex is locked */
1450 connection_closing( c, "SASL layer install failure" );
1451 connection_close( c );
1452 connection_return( c );
1458 #define CONNECTION_INPUT_LOOP 1
1459 /* #define DATA_READY_LOOP 1 */
1462 /* How do we do this without getting into a busy loop ? */
1463 rc = connection_input( c, cri );
1465 #ifdef DATA_READY_LOOP
1466 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1467 #elif defined CONNECTION_INPUT_LOOP
1474 Debug( LDAP_DEBUG_CONNS,
1475 "connection_read(%d): input error=%d id=%lu, closing.\n",
1476 s, rc, c->c_connid );
1478 /* c_mutex is locked */
1479 connection_closing( c, conn_lost_str );
1480 connection_close( c );
1481 connection_return( c );
1485 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1486 slapd_set_write( s, 0 );
1489 slapd_set_read( s, 1 );
1490 connection_return( c );
1496 connection_input( Connection *conn , conn_readinfo *cri )
1504 #ifdef LDAP_CONNECTIONLESS
1511 if ( conn->c_currentber == NULL &&
1512 ( conn->c_currentber = ber_alloc()) == NULL )
1514 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1520 #ifdef LDAP_CONNECTIONLESS
1521 if ( conn->c_is_udp ) {
1522 #if defined(LDAP_PF_INET6)
1523 char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
1524 char addr[INET6_ADDRSTRLEN];
1526 char peername[sizeof("IP=255.255.255.255:65336")];
1527 char addr[INET_ADDRSTRLEN];
1529 const char *peeraddr_string = NULL;
1531 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(Sockaddr));
1532 if (len != sizeof(Sockaddr)) return 1;
1534 #if defined(LDAP_PF_INET6)
1535 if (peeraddr.sa_addr.sa_family == AF_INET6) {
1536 if ( IN6_IS_ADDR_V4MAPPED(&peeraddr.sa_in6_addr.sin6_addr) ) {
1537 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1538 peeraddr_string = inet_ntop( AF_INET,
1539 ((struct in_addr *)&peeraddr.sa_in6_addr.sin6_addr.s6_addr[12]),
1540 addr, sizeof(addr) );
1541 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1542 peeraddr_string = inet_ntoa( *((struct in_addr *)
1543 &peeraddr.sa_in6_addr.sin6_addr.s6_addr[12]) );
1544 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1545 if ( !peeraddr_string ) peeraddr_string = SLAP_STRING_UNKNOWN;
1546 sprintf( peername, "IP=%s:%d", peeraddr_string,
1547 (unsigned) ntohs( peeraddr.sa_in6_addr.sin6_port ) );
1549 peeraddr_string = inet_ntop( AF_INET6,
1550 &peeraddr.sa_in6_addr.sin6_addr,
1551 addr, sizeof addr );
1552 if ( !peeraddr_string ) peeraddr_string = SLAP_STRING_UNKNOWN;
1553 sprintf( peername, "IP=[%s]:%d", peeraddr_string,
1554 (unsigned) ntohs( peeraddr.sa_in6_addr.sin6_port ) );
1558 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1560 peeraddr_string = inet_ntop( AF_INET, &peeraddr.sa_in_addr.sin_addr,
1561 addr, sizeof(addr) );
1562 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1563 peeraddr_string = inet_ntoa( peeraddr.sa_in_addr.sin_addr );
1564 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1565 sprintf( peername, "IP=%s:%d",
1567 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1569 Statslog( LDAP_DEBUG_STATS,
1570 "conn=%lu UDP request from %s (%s) accepted.\n",
1571 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1575 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1576 if ( tag != LDAP_TAG_MESSAGE ) {
1577 int err = sock_errno();
1579 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1580 /* log, close and send error */
1581 Debug( LDAP_DEBUG_TRACE,
1582 "ber_get_next on fd %d failed errno=%d (%s)\n",
1583 conn->c_sd, err, sock_errstr(err) );
1584 ber_free( conn->c_currentber, 1 );
1585 conn->c_currentber = NULL;
1592 ber = conn->c_currentber;
1593 conn->c_currentber = NULL;
1595 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1596 /* log, close and send error */
1597 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1602 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1603 /* log, close and send error */
1604 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1610 #ifdef LDAP_CONNECTIONLESS
1611 if( conn->c_is_udp ) {
1612 if( tag == LBER_OCTETSTRING ) {
1613 if ( (tag = ber_get_stringa( ber, &cdn )) != LBER_ERROR )
1614 tag = ber_peek_tag( ber, &len );
1616 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1617 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1624 if(tag == LDAP_REQ_BIND) {
1625 /* immediately abandon all existing operations upon BIND */
1626 connection_abandon( conn );
1630 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++, ctx );
1632 Debug( LDAP_DEBUG_TRACE, "op tag 0x%lx, time %ld\n", tag,
1633 (long) op->o_time, 0);
1636 /* clear state if the connection is being reused from inactive */
1637 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1638 memset( &conn->c_pagedresults_state, 0,
1639 sizeof( conn->c_pagedresults_state ) );
1642 op->o_res_ber = NULL;
1644 #ifdef LDAP_CONNECTIONLESS
1645 if (conn->c_is_udp) {
1647 ber_str2bv( cdn, 0, 1, &op->o_dn );
1648 op->o_protocol = LDAP_VERSION2;
1650 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1651 if (op->o_res_ber == NULL) return 1;
1653 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1654 sizeof(struct sockaddr), 0 );
1656 if (rc != sizeof(struct sockaddr)) {
1657 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1661 if (op->o_protocol == LDAP_VERSION2) {
1662 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1664 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1669 #endif /* LDAP_CONNECTIONLESS */
1673 /* Don't process requests when the conn is in the middle of a
1674 * Bind, or if it's closing. Also, don't let any single conn
1675 * use up all the available threads, and don't execute if we're
1676 * currently blocked on output. And don't execute if there are
1677 * already pending ops, let them go first. Abandon operations
1678 * get exceptions to some, but not all, cases.
1682 /* Abandon and Unbind are exempt from these checks */
1683 if (conn->c_conn_state == SLAP_C_CLOSING) {
1686 } else if (conn->c_writewaiter) {
1687 defer = "awaiting write";
1689 } else if (conn->c_n_ops_pending) {
1690 defer = "pending operations";
1694 case LDAP_REQ_ABANDON:
1695 /* Unbind is exempt from these checks */
1696 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1697 defer = "too many executing";
1699 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1704 case LDAP_REQ_UNBIND:
1709 int max = conn->c_dn.bv_len
1710 ? slap_conn_max_pending_auth
1711 : slap_conn_max_pending;
1713 Debug( LDAP_DEBUG_ANY,
1714 "connection_input: conn=%lu deferring operation: %s\n",
1715 conn->c_connid, defer, 0 );
1716 conn->c_n_ops_pending++;
1717 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1718 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1721 conn->c_n_ops_executing++;
1724 * The first op will be processed in the same thread context,
1725 * as long as there is only one op total.
1726 * Subsequent ops will be submitted to the pool by
1727 * calling connection_op_activate()
1729 if ( cri->op == NULL ) {
1730 /* the first incoming request */
1731 connection_op_queue( op );
1734 if ( !cri->nullop ) {
1736 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1737 connection_operation, (void *) cri->op );
1739 connection_op_activate( op );
1744 if ( conn->c_struct_state != SLAP_C_USED ) {
1745 /* connection must have got closed underneath us */
1750 assert( conn->c_struct_state == SLAP_C_USED );
1755 connection_resched( Connection *conn )
1759 if( conn->c_writewaiter )
1762 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1763 Debug( LDAP_DEBUG_CONNS, "connection_resched: "
1764 "attempting closing conn=%lu sd=%d\n",
1765 conn->c_connid, conn->c_sd, 0 );
1766 connection_close( conn );
1770 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1771 /* other states need different handling */
1775 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1776 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1778 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1779 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1781 /* pending operations should not be marked for abandonment */
1782 assert(!op->o_abandon);
1784 conn->c_n_ops_pending--;
1785 conn->c_n_ops_executing++;
1787 connection_op_activate( op );
1789 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1795 connection_init_log_prefix( Operation *op )
1797 if ( op->o_connid == (unsigned long)(-1) ) {
1798 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1799 "conn=-1 op=%lu", op->o_opid );
1802 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1803 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1807 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1809 op->o_conn->c_sasl_bindop = NULL;
1811 ch_free( op->o_callback );
1812 op->o_callback = NULL;
1814 return SLAP_CB_CONTINUE;
1817 static int connection_bind_cb( Operation *op, SlapReply *rs )
1819 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1820 op->o_conn->c_sasl_bind_in_progress =
1821 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1823 /* Moved here from bind.c due to ITS#4158 */
1824 op->o_conn->c_sasl_bindop = NULL;
1825 if ( op->orb_method == LDAP_AUTH_SASL ) {
1826 if( rs->sr_err == LDAP_SUCCESS ) {
1827 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1828 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1829 /* edn is always normalized already */
1830 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1832 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1833 BER_BVZERO( &op->orb_edn );
1834 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1835 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1837 op->o_conn->c_sasl_ssf = op->orb_ssf;
1838 if( op->orb_ssf > op->o_conn->c_ssf ) {
1839 op->o_conn->c_ssf = op->orb_ssf;
1842 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1843 ber_len_t max = sockbuf_max_incoming_auth;
1844 ber_sockbuf_ctrl( op->o_conn->c_sb,
1845 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1848 /* log authorization identity */
1849 Statslog( LDAP_DEBUG_STATS,
1850 "%s BIND dn=\"%s\" mech=%s sasl_ssf=%d ssf=%d\n",
1852 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1853 op->o_conn->c_authmech.bv_val,
1854 op->orb_ssf, op->o_conn->c_ssf );
1856 Debug( LDAP_DEBUG_TRACE,
1857 "do_bind: SASL/%s bind: dn=\"%s\" sasl_ssf=%d\n",
1858 op->o_conn->c_authmech.bv_val,
1859 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1862 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1863 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1864 free( op->o_conn->c_sasl_bind_mech.bv_val );
1865 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1869 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1871 ch_free( op->o_callback );
1872 op->o_callback = NULL;
1874 return SLAP_CB_CONTINUE;
1877 static void connection_op_queue( Operation *op )
1879 ber_tag_t tag = op->o_tag;
1881 if (tag == LDAP_REQ_BIND) {
1882 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1883 sc->sc_response = connection_bind_cb;
1884 sc->sc_cleanup = connection_bind_cleanup_cb;
1885 sc->sc_next = op->o_callback;
1886 op->o_callback = sc;
1887 op->o_conn->c_conn_state = SLAP_C_BINDING;
1890 if (!op->o_dn.bv_len) {
1891 op->o_authz = op->o_conn->c_authz;
1892 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1893 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1894 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1896 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1897 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1901 op->o_authtype = op->o_conn->c_authtype;
1902 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1904 if (!op->o_protocol) {
1905 op->o_protocol = op->o_conn->c_protocol
1906 ? op->o_conn->c_protocol : LDAP_VERSION3;
1909 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1910 op->o_protocol > LDAP_VERSION2)
1912 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1915 op->o_connid = op->o_conn->c_connid;
1916 connection_init_log_prefix( op );
1918 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1921 static int connection_op_activate( Operation *op )
1925 connection_op_queue( op );
1927 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1928 connection_operation, (void *) op );
1931 Debug( LDAP_DEBUG_ANY,
1932 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1933 rc, op->o_connid, 0 );
1934 /* should move op to pending list */
1940 int connection_write(ber_socket_t s)
1945 assert( connections != NULL );
1947 c = connection_get( s );
1949 Debug( LDAP_DEBUG_ANY,
1950 "connection_write(%ld): no connection!\n",
1955 slapd_clr_write( s, 0 );
1958 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1959 connection_return( c );
1960 connection_read_activate( s );
1967 Debug( LDAP_DEBUG_TRACE,
1968 "connection_write(%d): waking output for id=%lu\n",
1969 s, c->c_connid, 0 );
1970 ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
1971 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
1972 ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
1974 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1975 slapd_set_read( s, 1 );
1977 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1978 slapd_set_write( s, 1 );
1981 /* If there are ops pending because of a writewaiter,
1984 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1985 if ( !c->c_writewaiter ) break;
1986 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1988 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1989 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1991 /* pending operations should not be marked for abandonment */
1992 assert(!op->o_abandon);
1994 c->c_n_ops_pending--;
1995 c->c_n_ops_executing++;
1997 connection_op_activate( op );
2002 connection_return( c );
2007 typedef struct conn_fake_extblock {
2010 } conn_fake_extblock;
2013 connection_fake_destroy(
2017 Connection conn = {0};
2019 Opheader ohdr = {0};
2021 conn_fake_extblock *eb = data;
2024 op.o_hdr->oh_extensions = eb->eb_op;
2025 conn.c_extensions = eb->eb_conn;
2030 ber_memfree_x( eb, NULL );
2031 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
2032 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
2037 connection_fake_init(
2039 OperationBuffer *opbuf,
2042 connection_fake_init2( conn, opbuf, ctx, 1 );
2046 operation_fake_init(
2052 /* set memory context */
2053 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
2055 op->o_tmpmfuncs = &slap_sl_mfuncs;
2056 op->o_threadctx = ctx;
2057 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
2059 op->o_counters = &slap_counters;
2061 op->o_connid = op->o_conn->c_connid;
2062 connection_init_log_prefix( op );
2067 connection_fake_init2(
2069 OperationBuffer *opbuf,
2073 Operation *op = (Operation *) opbuf;
2075 conn->c_connid = -1;
2076 conn->c_conn_idx = -1;
2077 conn->c_send_ldap_result = slap_send_ldap_result;
2078 conn->c_send_search_entry = slap_send_search_entry;
2079 conn->c_send_search_reference = slap_send_search_reference;
2080 conn->c_send_ldap_extended = slap_send_ldap_extended;
2081 conn->c_send_ldap_intermediate = slap_send_ldap_intermediate;
2082 conn->c_listener = (Listener *)&dummy_list;
2083 conn->c_peer_domain = slap_empty_bv;
2084 conn->c_peer_name = slap_empty_bv;
2086 memset( opbuf, 0, sizeof( *opbuf ));
2087 op->o_hdr = &opbuf->ob_hdr;
2088 op->o_controls = opbuf->ob_controls;
2090 operation_fake_init( conn, op, ctx, newmem );
2093 if ( slapi_plugins_used ) {
2094 conn_fake_extblock *eb;
2097 /* Use thread keys to make sure these eventually get cleaned up */
2098 if ( ldap_pvt_thread_pool_getkey( ctx, (void *)connection_fake_init,
2100 eb = ch_malloc( sizeof( *eb ));
2101 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2102 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2103 eb->eb_conn = conn->c_extensions;
2104 eb->eb_op = op->o_hdr->oh_extensions;
2105 ldap_pvt_thread_pool_setkey( ctx, (void *)connection_fake_init,
2106 eb, connection_fake_destroy, NULL, NULL );
2109 conn->c_extensions = eb->eb_conn;
2110 op->o_hdr->oh_extensions = eb->eb_op;
2113 #endif /* LDAP_SLAPI */
2115 slap_op_time( &op->o_time, &op->o_tincr );
2119 connection_assign_nextid( Connection *conn )
2121 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2122 conn->c_connid = conn_nextid++;
2123 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );