2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 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_cond_destroy( &connections[i].c_write1_cv );
159 if ( slapi_plugins_used ) {
160 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
170 ldap_pvt_thread_mutex_destroy( &connections_mutex );
171 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
176 * shutdown all connections
178 int connections_shutdown(void)
182 for ( i = 0; i < dtblsize; i++ ) {
183 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
184 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
185 if( connections[i].c_struct_state == SLAP_C_USED ) {
187 /* give persistent clients a chance to cleanup */
188 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
189 ldap_pvt_thread_pool_submit( &connection_pool,
190 connections[i].c_clientfunc, connections[i].c_clientarg );
192 /* c_mutex is locked */
193 connection_closing( &connections[i], "slapd shutdown" );
194 connection_close( &connections[i] );
197 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
205 * Timeout idle connections.
207 int connections_timeout_idle(time_t now)
209 int i = 0, writers = 0;
210 ber_socket_t connindex;
213 for( c = connection_first( &connindex );
215 c = connection_next( c, &connindex ) )
217 /* Don't timeout a slow-running request or a persistent
218 * outbound connection.
220 if(( c->c_n_ops_executing && !c->c_writewaiter)
221 || c->c_conn_state == SLAP_C_CLIENT ) {
225 if( global_idletimeout &&
226 difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
228 connection_closing( c, "idletimeout" );
229 connection_close( c );
234 connection_done( c );
239 /* Drop all client connections */
240 void connections_drop()
243 ber_socket_t connindex;
245 for( c = connection_first( &connindex );
247 c = connection_next( c, &connindex ) )
249 /* Don't close a slow-running request or a persistent
250 * outbound connection.
252 if(( c->c_n_ops_executing && !c->c_writewaiter)
253 || c->c_conn_state == SLAP_C_CLIENT ) {
256 connection_closing( c, "dropping" );
257 connection_close( c );
259 connection_done( c );
262 static Connection* connection_get( ber_socket_t s )
266 Debug( LDAP_DEBUG_ARGS,
267 "connection_get(%ld)\n",
270 assert( connections != NULL );
272 if(s == AC_SOCKET_INVALID) return NULL;
274 assert( s < dtblsize );
278 ldap_pvt_thread_mutex_lock( &c->c_mutex );
280 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
282 if( c->c_struct_state != SLAP_C_USED ) {
283 /* connection must have been closed due to resched */
285 Debug( LDAP_DEBUG_CONNS,
286 "connection_get(%d): connection not used\n",
288 assert( c->c_conn_state == SLAP_C_INVALID );
289 assert( c->c_sd == AC_SOCKET_INVALID );
291 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
295 Debug( LDAP_DEBUG_TRACE,
296 "connection_get(%d): got connid=%lu\n",
301 assert( c->c_struct_state == SLAP_C_USED );
302 assert( c->c_conn_state != SLAP_C_INVALID );
303 assert( c->c_sd != AC_SOCKET_INVALID );
305 #ifndef SLAPD_MONITOR
306 if ( global_idletimeout > 0 )
307 #endif /* ! SLAPD_MONITOR */
309 c->c_activitytime = slap_get_time();
316 static void connection_return( Connection *c )
318 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
321 Connection * connection_init(
325 const char* peername,
328 struct berval *authid
329 LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
334 ber_socket_t sfd = SLAP_FD2SOCK(s);
336 assert( connections != NULL );
338 assert( listener != NULL );
339 assert( dnsname != NULL );
340 assert( peername != NULL );
343 assert( !( flags & CONN_IS_TLS ));
346 if( s == AC_SOCKET_INVALID ) {
347 Debug( LDAP_DEBUG_ANY,
348 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
353 assert( s < dtblsize );
355 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
358 assert( c->c_struct_state == SLAP_C_UNUSED );
362 c->c_send_ldap_result = slap_send_ldap_result;
363 c->c_send_search_entry = slap_send_search_entry;
364 c->c_send_search_reference = slap_send_search_reference;
365 c->c_send_ldap_extended = slap_send_ldap_extended;
366 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
368 BER_BVZERO( &c->c_authmech );
369 BER_BVZERO( &c->c_dn );
370 BER_BVZERO( &c->c_ndn );
372 c->c_listener = NULL;
373 BER_BVZERO( &c->c_peer_domain );
374 BER_BVZERO( &c->c_peer_name );
376 LDAP_STAILQ_INIT(&c->c_ops);
377 LDAP_STAILQ_INIT(&c->c_pending_ops);
380 c->c_txn = CONN_TXN_INACTIVE;
381 c->c_txn_backend = NULL;
382 LDAP_STAILQ_INIT(&c->c_txn_ops);
385 BER_BVZERO( &c->c_sasl_bind_mech );
387 c->c_sasl_authctx = NULL;
388 c->c_sasl_sockctx = NULL;
389 c->c_sasl_extra = NULL;
390 c->c_sasl_bindop = NULL;
391 c->c_sasl_cbind = NULL;
393 c->c_sb = ber_sockbuf_alloc( );
396 ber_len_t max = sockbuf_max_incoming;
397 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
400 c->c_currentber = NULL;
402 /* should check status of thread calls */
403 ldap_pvt_thread_mutex_init( &c->c_mutex );
404 ldap_pvt_thread_mutex_init( &c->c_write1_mutex );
405 ldap_pvt_thread_cond_init( &c->c_write1_cv );
408 if ( slapi_plugins_used ) {
409 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
414 ldap_pvt_thread_mutex_lock( &c->c_mutex );
416 assert( BER_BVISNULL( &c->c_authmech ) );
417 assert( BER_BVISNULL( &c->c_dn ) );
418 assert( BER_BVISNULL( &c->c_ndn ) );
419 assert( c->c_listener == NULL );
420 assert( BER_BVISNULL( &c->c_peer_domain ) );
421 assert( BER_BVISNULL( &c->c_peer_name ) );
422 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
423 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
425 assert( c->c_txn == CONN_TXN_INACTIVE );
426 assert( c->c_txn_backend == NULL );
427 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
429 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
430 assert( c->c_sasl_done == 0 );
431 assert( c->c_sasl_authctx == NULL );
432 assert( c->c_sasl_sockctx == NULL );
433 assert( c->c_sasl_extra == NULL );
434 assert( c->c_sasl_bindop == NULL );
435 assert( c->c_sasl_cbind == NULL );
436 assert( c->c_currentber == NULL );
437 assert( c->c_writewaiter == 0);
438 assert( c->c_writers == 0);
440 c->c_listener = listener;
443 if ( flags & CONN_IS_CLIENT ) {
445 ldap_pvt_thread_mutex_lock( &connections_mutex );
446 c->c_conn_state = SLAP_C_CLIENT;
447 c->c_struct_state = SLAP_C_USED;
448 ldap_pvt_thread_mutex_unlock( &connections_mutex );
449 c->c_close_reason = "?"; /* should never be needed */
450 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
451 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
456 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
457 ber_str2bv( peername, 0, 1, &c->c_peer_name );
459 c->c_n_ops_received = 0;
460 c->c_n_ops_executing = 0;
461 c->c_n_ops_pending = 0;
462 c->c_n_ops_completed = 0;
468 /* set to zero until bind, implies LDAP_VERSION3 */
471 #ifndef SLAPD_MONITOR
472 if ( global_idletimeout > 0 )
473 #endif /* ! SLAPD_MONITOR */
475 c->c_activitytime = c->c_starttime = slap_get_time();
478 #ifdef LDAP_CONNECTIONLESS
480 if( flags & CONN_IS_UDP ) {
483 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
484 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
486 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
487 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
488 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
489 LBER_SBIOD_LEVEL_PROVIDER, NULL );
491 #endif /* LDAP_CONNECTIONLESS */
493 if ( flags & CONN_IS_IPC ) {
495 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
496 LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
498 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
499 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
500 #ifdef LDAP_PF_LOCAL_SENDMSG
501 if ( !BER_BVISEMPTY( peerbv ))
502 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
505 #endif /* LDAP_PF_LOCAL */
508 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
509 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
511 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
512 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
516 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
517 INT_MAX, (void*)"ldap_" );
520 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
521 c /* non-NULL */ ) < 0 )
523 Debug( LDAP_DEBUG_ANY,
524 "connection_init(%d, %s): set nonblocking failed\n",
525 s, c->c_peer_name.bv_val, 0 );
528 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
529 id = c->c_connid = conn_nextid++;
530 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
532 ldap_pvt_thread_mutex_lock( &connections_mutex );
533 c->c_conn_state = SLAP_C_INACTIVE;
534 c->c_struct_state = SLAP_C_USED;
535 ldap_pvt_thread_mutex_unlock( &connections_mutex );
536 c->c_close_reason = "?"; /* should never be needed */
538 c->c_ssf = c->c_transport_ssf = ssf;
542 if ( flags & CONN_IS_TLS ) {
544 c->c_needs_tls_accept = 1;
547 c->c_needs_tls_accept = 0;
551 slap_sasl_open( c, 0 );
552 slap_sasl_external( c, ssf, authid );
554 slapd_add_internal( s, 1 );
556 backend_connection_init(c);
557 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
559 if ( !(flags & CONN_IS_UDP ))
560 Statslog( LDAP_DEBUG_STATS,
561 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
562 id, (long) s, peername, listener->sl_name.bv_val, 0 );
567 void connection2anonymous( Connection *c )
569 assert( connections != NULL );
573 ber_len_t max = sockbuf_max_incoming;
574 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
577 if ( !BER_BVISNULL( &c->c_authmech ) ) {
578 ch_free(c->c_authmech.bv_val);
580 BER_BVZERO( &c->c_authmech );
582 if ( !BER_BVISNULL( &c->c_dn ) ) {
583 ch_free(c->c_dn.bv_val);
585 BER_BVZERO( &c->c_dn );
587 if ( !BER_BVISNULL( &c->c_ndn ) ) {
588 ch_free(c->c_ndn.bv_val);
590 BER_BVZERO( &c->c_ndn );
592 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
593 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
595 BER_BVZERO( &c->c_sasl_authz_dn );
597 c->c_authz_backend = NULL;
601 connection_destroy( Connection *c )
603 unsigned long connid;
604 const char *close_reason;
608 assert( connections != NULL );
610 assert( c->c_struct_state != SLAP_C_UNUSED );
611 assert( c->c_conn_state != SLAP_C_INVALID );
612 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
613 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
615 assert( c->c_txn == CONN_TXN_INACTIVE );
616 assert( c->c_txn_backend == NULL );
617 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
619 assert( c->c_writewaiter == 0);
620 assert( c->c_writers == 0);
622 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
623 connid = c->c_connid;
624 close_reason = c->c_close_reason;
626 ldap_pvt_thread_mutex_lock( &connections_mutex );
627 c->c_struct_state = SLAP_C_PENDING;
628 ldap_pvt_thread_mutex_unlock( &connections_mutex );
630 backend_connection_destroy(c);
635 c->c_activitytime = c->c_starttime = 0;
637 connection2anonymous( c );
638 c->c_listener = NULL;
640 if(c->c_peer_domain.bv_val != NULL) {
641 free(c->c_peer_domain.bv_val);
643 BER_BVZERO( &c->c_peer_domain );
644 if(c->c_peer_name.bv_val != NULL) {
645 free(c->c_peer_name.bv_val);
647 BER_BVZERO( &c->c_peer_name );
649 c->c_sasl_bind_in_progress = 0;
650 if(c->c_sasl_bind_mech.bv_val != NULL) {
651 free(c->c_sasl_bind_mech.bv_val);
653 BER_BVZERO( &c->c_sasl_bind_mech );
655 slap_sasl_close( c );
657 if ( c->c_currentber != NULL ) {
658 ber_free( c->c_currentber, 1 );
659 c->c_currentber = NULL;
664 /* call destructors, then constructors; avoids unnecessary allocation */
665 if ( slapi_plugins_used ) {
666 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
671 c->c_sd = AC_SOCKET_INVALID;
672 c->c_close_reason = "?"; /* should never be needed */
675 c->c_sb = ber_sockbuf_alloc( );
677 ber_len_t max = sockbuf_max_incoming;
678 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
680 c->c_conn_state = SLAP_C_INVALID;
681 c->c_struct_state = SLAP_C_UNUSED;
683 /* c must be fully reset by this point; when we call slapd_remove
684 * it may get immediately reused by a new connection.
686 if ( sd != AC_SOCKET_INVALID ) {
687 slapd_remove( sd, sb, 1, 0, 0 );
689 if ( close_reason == NULL ) {
690 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
691 connid, (long) sd, 0, 0, 0 );
693 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
694 connid, (long) sd, close_reason, 0, 0 );
699 int connection_valid( Connection *c )
701 /* c_mutex must be locked by caller */
705 return c->c_struct_state == SLAP_C_USED &&
706 c->c_conn_state >= SLAP_C_ACTIVE &&
707 c->c_conn_state <= SLAP_C_CLIENT;
710 static void connection_abandon( Connection *c )
712 /* c_mutex must be locked by caller */
714 Operation *o, *next, op = {0};
719 op.o_connid = c->c_connid;
720 op.o_tag = LDAP_REQ_ABANDON;
722 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
723 SlapReply rs = {REP_RESULT};
725 next = LDAP_STAILQ_NEXT( o, o_next );
726 /* don't abandon an op twice */
729 op.orn_msgid = o->o_msgid;
731 op.o_bd = frontendDB;
732 frontendDB->be_abandon( &op, &rs );
736 /* remove operations in pending transaction */
737 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
738 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
739 LDAP_STAILQ_NEXT(o, o_next) = NULL;
740 slap_op_free( o, NULL );
743 /* clear transaction */
744 c->c_txn_backend = NULL;
745 c->c_txn = CONN_TXN_INACTIVE;
748 /* remove pending operations */
749 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
750 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
751 LDAP_STAILQ_NEXT(o, o_next) = NULL;
752 slap_op_free( o, NULL );
757 connection_wake_writers( Connection *c )
759 /* wake write blocked operations */
760 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
761 if ( c->c_writers > 0 ) {
762 c->c_writers = -c->c_writers;
763 ldap_pvt_thread_cond_broadcast( &c->c_write1_cv );
764 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
765 if ( c->c_writewaiter ) {
766 slapd_shutsock( c->c_sd );
768 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
769 while ( c->c_writers ) {
770 ldap_pvt_thread_cond_wait( &c->c_write1_cv, &c->c_write1_mutex );
772 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
774 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
775 slapd_clr_write( c->c_sd, 1 );
779 void connection_closing( Connection *c, const char *why )
781 assert( connections != NULL );
784 if ( c->c_struct_state != SLAP_C_USED ) return;
786 assert( c->c_conn_state != SLAP_C_INVALID );
788 /* c_mutex must be locked by caller */
790 if( c->c_conn_state != SLAP_C_CLOSING ) {
791 Debug( LDAP_DEBUG_CONNS,
792 "connection_closing: readying conn=%lu sd=%d for close\n",
793 c->c_connid, c->c_sd, 0 );
794 /* update state to closing */
795 c->c_conn_state = SLAP_C_CLOSING;
796 c->c_close_reason = why;
798 /* don't listen on this port anymore */
799 slapd_clr_read( c->c_sd, 0 );
801 /* abandon active operations */
802 connection_abandon( c );
804 /* wake write blocked operations */
805 connection_wake_writers( c );
807 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
808 /* Client closed connection after doing Unbind. */
809 c->c_close_reason = NULL;
814 connection_close( Connection *c )
816 assert( connections != NULL );
819 if ( c->c_struct_state != SLAP_C_USED ) return;
821 assert( c->c_conn_state == SLAP_C_CLOSING );
823 /* NOTE: c_mutex should be locked by caller */
825 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
826 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
828 Debug( LDAP_DEBUG_CONNS,
829 "connection_close: deferring conn=%lu sd=%d\n",
830 c->c_connid, c->c_sd, 0 );
834 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
835 c->c_connid, c->c_sd, 0 );
837 connection_destroy( c );
840 unsigned long connections_nextid(void)
843 assert( connections != NULL );
845 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
849 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
855 * Loop through the connections:
857 * for (c = connection_first(&i); c; c = connection_next(c, &i)) ...;
858 * connection_done(c);
860 * 'i' is the cursor, initialized by connection_first().
861 * 'c_mutex' is locked in the returned connection. The functions must
862 * be passed the previous return value so they can unlock it again.
865 Connection* connection_first( ber_socket_t *index )
867 assert( connections != NULL );
868 assert( index != NULL );
870 ldap_pvt_thread_mutex_lock( &connections_mutex );
871 for( *index = 0; *index < dtblsize; (*index)++) {
872 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
876 ldap_pvt_thread_mutex_unlock( &connections_mutex );
878 return connection_next(NULL, index);
881 /* Next connection in loop, see connection_first() */
882 Connection* connection_next( Connection *c, ber_socket_t *index )
884 assert( connections != NULL );
885 assert( index != NULL );
886 assert( *index <= dtblsize );
888 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
892 ldap_pvt_thread_mutex_lock( &connections_mutex );
893 for(; *index < dtblsize; (*index)++) {
895 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
896 /* FIXME: accessing c_conn_state without locking c_mutex */
897 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
901 if( connections[*index].c_struct_state == SLAP_C_USED ) {
902 c = &connections[(*index)++];
903 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
905 ldap_pvt_thread_mutex_unlock( &connections_mutex );
906 ldap_pvt_thread_mutex_lock( &c->c_mutex );
907 ldap_pvt_thread_mutex_lock( &connections_mutex );
908 if ( c->c_struct_state != SLAP_C_USED ) {
909 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
914 assert( c->c_conn_state != SLAP_C_INVALID );
918 c_struct = connections[*index].c_struct_state;
919 if ( c_struct == SLAP_C_PENDING )
921 assert( c_struct == SLAP_C_UNUSED );
922 /* FIXME: accessing c_conn_state without locking c_mutex */
923 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
926 ldap_pvt_thread_mutex_unlock( &connections_mutex );
930 /* End connection loop, see connection_first() */
931 void connection_done( Connection *c )
933 assert( connections != NULL );
935 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
939 * connection_activity - handle the request operation op on connection
940 * conn. This routine figures out what kind of operation it is and
941 * calls the appropriate stub to handle it.
945 /* FIXME: returns 0 in case of failure */
946 #define INCR_OP_INITIATED(index) \
948 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
949 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated_[(index)], 1); \
950 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
952 #define INCR_OP_COMPLETED(index) \
954 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
955 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
956 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed_[(index)], 1); \
957 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
959 #else /* !SLAPD_MONITOR */
960 #define INCR_OP_INITIATED(index) do { } while (0)
961 #define INCR_OP_COMPLETED(index) \
963 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
964 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
965 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
967 #endif /* !SLAPD_MONITOR */
970 * NOTE: keep in sync with enum in slapd.h
972 static BI_op_func *opfun[] = {
986 /* Counters are per-thread, not per-connection.
989 conn_counter_destroy( void *key, void *data )
991 slap_counters_t **prev, *sc;
993 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
994 for ( prev = &slap_counters.sc_next, sc = slap_counters.sc_next; sc;
995 prev = &sc->sc_next, sc = sc->sc_next ) {
1000 /* Copy data to main counter */
1001 ldap_pvt_mp_add( slap_counters.sc_bytes, sc->sc_bytes );
1002 ldap_pvt_mp_add( slap_counters.sc_pdu, sc->sc_pdu );
1003 ldap_pvt_mp_add( slap_counters.sc_entries, sc->sc_entries );
1004 ldap_pvt_mp_add( slap_counters.sc_refs, sc->sc_refs );
1005 ldap_pvt_mp_add( slap_counters.sc_ops_initiated, sc->sc_ops_initiated );
1006 ldap_pvt_mp_add( slap_counters.sc_ops_completed, sc->sc_ops_completed );
1007 #ifdef SLAPD_MONITOR
1008 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
1009 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_initiated_[ i ] );
1010 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_completed_[ i ] );
1012 #endif /* SLAPD_MONITOR */
1013 slap_counters_destroy( sc );
1014 ber_memfree_x( data, NULL );
1018 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1022 conn_counter_init( Operation *op, void *ctx )
1024 slap_counters_t *sc;
1027 if ( ldap_pvt_thread_pool_getkey(
1028 ctx, (void *)conn_counter_init, &vsc, NULL ) || !vsc ) {
1029 vsc = ch_malloc( sizeof( slap_counters_t ));
1031 slap_counters_init( sc );
1032 ldap_pvt_thread_pool_setkey( ctx, (void*)conn_counter_init, vsc,
1033 conn_counter_destroy, NULL, NULL );
1035 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
1036 sc->sc_next = slap_counters.sc_next;
1037 slap_counters.sc_next = sc;
1038 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1040 op->o_counters = vsc;
1044 connection_op_finish( Operation *op )
1046 Connection *conn = op->o_conn;
1047 void *memctx_null = NULL;
1049 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1051 if ( op->o_tag == LDAP_REQ_BIND && conn->c_conn_state == SLAP_C_BINDING )
1052 conn->c_conn_state = SLAP_C_ACTIVE;
1054 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1056 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1057 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1058 conn->c_n_ops_executing--;
1059 conn->c_n_ops_completed++;
1060 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1064 connection_operation( void *ctx, void *arg_v )
1066 int rc = LDAP_OTHER, cancel;
1067 Operation *op = arg_v;
1068 SlapReply rs = {REP_RESULT};
1069 ber_tag_t tag = op->o_tag;
1070 slap_op_t opidx = SLAP_OP_LAST;
1071 Connection *conn = op->o_conn;
1072 void *memctx = NULL;
1073 void *memctx_null = NULL;
1076 gettimeofday( &op->o_qtime, NULL );
1077 op->o_qtime.tv_usec -= op->o_tusec;
1078 if ( op->o_qtime.tv_usec < 0 ) {
1079 op->o_qtime.tv_usec += 1000000;
1080 op->o_qtime.tv_sec--;
1082 op->o_qtime.tv_sec -= op->o_time;
1083 conn_counter_init( op, ctx );
1084 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1085 /* FIXME: returns 0 in case of failure */
1086 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated, 1);
1087 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1089 op->o_threadctx = ctx;
1090 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1094 case LDAP_REQ_UNBIND:
1096 case LDAP_REQ_DELETE:
1097 case LDAP_REQ_MODDN:
1098 case LDAP_REQ_MODIFY:
1099 case LDAP_REQ_COMPARE:
1100 case LDAP_REQ_SEARCH:
1101 case LDAP_REQ_ABANDON:
1102 case LDAP_REQ_EXTENDED:
1105 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1106 "conn %lu unknown LDAP request 0x%lx\n",
1107 conn->c_connid, tag, 0 );
1108 op->o_tag = LBER_ERROR;
1109 rs.sr_err = LDAP_PROTOCOL_ERROR;
1110 rs.sr_text = "unknown LDAP request";
1111 send_ldap_disconnect( op, &rs );
1112 rc = SLAPD_DISCONNECT;
1113 goto operations_error;
1116 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1117 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1118 "error: SASL bind in progress (tag=%ld).\n",
1120 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1121 "SASL bind in progress" );
1122 rc = LDAP_OPERATIONS_ERROR;
1123 goto operations_error;
1127 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1128 ( tag == LDAP_REQ_ADD ) ||
1129 ( tag == LDAP_REQ_DELETE ) ||
1130 ( tag == LDAP_REQ_MODIFY ) ||
1131 ( tag == LDAP_REQ_MODRDN )))
1133 /* Disable SLAB allocator for all update operations
1134 issued inside of a transaction */
1135 op->o_tmpmemctx = NULL;
1136 op->o_tmpmfuncs = &ch_mfuncs;
1140 /* We can use Thread-Local storage for most mallocs. We can
1141 * also use TL for ber parsing, but not on Add or Modify.
1144 memsiz = ber_len( op->o_ber ) * 64;
1145 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1147 memsiz = SLAP_SLAB_SIZE;
1149 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1150 op->o_tmpmemctx = memctx;
1151 op->o_tmpmfuncs = &slap_sl_mfuncs;
1152 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1153 /* Note - the ber and its buffer are already allocated from
1154 * regular memory; this only affects subsequent mallocs that
1155 * ber_scanf may invoke.
1157 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1161 opidx = slap_req2op( tag );
1162 assert( opidx != SLAP_OP_LAST );
1163 INCR_OP_INITIATED( opidx );
1164 rc = (*(opfun[opidx]))( op, &rs );
1167 if ( rc == SLAPD_DISCONNECT ) {
1170 } else if ( rc == SLAPD_ASYNCOP ) {
1171 /* someone has claimed ownership of the op
1172 * to complete it later. Don't do anything
1173 * else with it now. Detach memctx too.
1175 slap_sl_mem_setctx( ctx, NULL );
1176 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1177 connection_resched( conn );
1178 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1181 } else if ( opidx != SLAP_OP_LAST ) {
1182 /* increment completed operations count
1183 * only if operation was initiated
1184 * and rc != SLAPD_DISCONNECT */
1185 INCR_OP_COMPLETED( opidx );
1188 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1190 if ( opidx == SLAP_OP_BIND && conn->c_conn_state == SLAP_C_BINDING )
1191 conn->c_conn_state = SLAP_C_ACTIVE;
1193 cancel = op->o_cancel;
1194 if ( cancel != SLAP_CANCEL_NONE && cancel != SLAP_CANCEL_DONE ) {
1195 if ( cancel == SLAP_CANCEL_REQ ) {
1196 op->o_cancel = rc == SLAPD_ABANDON
1197 ? SLAP_CANCEL_ACK : LDAP_TOO_LATE;
1201 /* Fake a cond_wait with thread_yield, then
1202 * verify the result properly mutex-protected.
1204 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1206 ldap_pvt_thread_yield();
1207 } while ( (cancel = op->o_cancel) != SLAP_CANCEL_NONE
1208 && cancel != SLAP_CANCEL_DONE );
1209 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1210 } while ( (cancel = op->o_cancel) != SLAP_CANCEL_NONE
1211 && cancel != SLAP_CANCEL_DONE );
1214 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1217 if ( rc != LDAP_X_TXN_SPECIFY_OKAY )
1220 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1221 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1223 conn->c_n_ops_executing--;
1224 conn->c_n_ops_completed++;
1228 case LDAP_REQ_UNBIND:
1229 /* c_mutex is locked */
1230 connection_closing( conn,
1231 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1235 connection_resched( conn );
1236 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1238 if ( rc != LDAP_X_TXN_SPECIFY_OKAY )
1241 slap_op_free( op, ctx );
1246 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1248 Connection *connection_client_setup(
1250 ldap_pvt_thread_start_t *func,
1254 ber_socket_t sfd = SLAP_SOCKNEW( s );
1256 c = connection_init( sfd, (Listener *)&dummy_list, "", "",
1257 CONN_IS_CLIENT, 0, NULL
1258 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
1260 c->c_clientfunc = func;
1261 c->c_clientarg = arg;
1263 slapd_add_internal( sfd, 0 );
1268 void connection_client_enable(
1271 slapd_set_read( c->c_sd, 1 );
1274 void connection_client_stop(
1278 ber_socket_t s = c->c_sd;
1280 /* get (locked) connection */
1281 c = connection_get( s );
1283 assert( c->c_conn_state == SLAP_C_CLIENT );
1285 c->c_listener = NULL;
1286 c->c_sd = AC_SOCKET_INVALID;
1287 c->c_close_reason = "?"; /* should never be needed */
1289 c->c_sb = ber_sockbuf_alloc( );
1291 ber_len_t max = sockbuf_max_incoming;
1292 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1294 c->c_conn_state = SLAP_C_INVALID;
1295 c->c_struct_state = SLAP_C_UNUSED;
1296 slapd_remove( s, sb, 0, 1, 0 );
1298 connection_return( c );
1301 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1303 static void* connection_read_thread( void* ctx, void* argv )
1306 conn_readinfo cri = { NULL, NULL, NULL, NULL, 0 };
1307 ber_socket_t s = (long)argv;
1310 * read incoming LDAP requests. If there is more than one,
1311 * the first one is returned with new_op
1314 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1315 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1316 return (void*)(long)rc;
1319 /* execute a single queued request in the same thread */
1320 if( cri.op && !cri.nullop ) {
1321 rc = (long)connection_operation( ctx, cri.op );
1322 } else if ( cri.func ) {
1323 rc = (long)cri.func( ctx, cri.arg );
1326 return (void*)(long)rc;
1329 int connection_read_activate( ber_socket_t s )
1334 * suspend reading on this file descriptor until a connection processing
1335 * thread reads data on it. Otherwise the listener thread will repeatedly
1336 * submit the same event on it to the pool.
1338 rc = slapd_clr_read( s, 0 );
1342 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1343 connection_read_thread, (void *)(long)s );
1346 Debug( LDAP_DEBUG_ANY,
1347 "connection_read_activate(%d): submit failed (%d)\n",
1355 connection_read( ber_socket_t s, conn_readinfo *cri )
1360 assert( connections != NULL );
1362 /* get (locked) connection */
1363 c = connection_get( s );
1366 Debug( LDAP_DEBUG_ANY,
1367 "connection_read(%ld): no connection!\n",
1375 if( c->c_conn_state == SLAP_C_CLOSING ) {
1376 Debug( LDAP_DEBUG_CONNS,
1377 "connection_read(%d): closing, ignoring input for id=%lu\n",
1378 s, c->c_connid, 0 );
1379 connection_return( c );
1383 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1384 cri->func = c->c_clientfunc;
1385 cri->arg = c->c_clientarg;
1386 /* read should already be cleared */
1387 connection_return( c );
1391 Debug( LDAP_DEBUG_TRACE,
1392 "connection_read(%d): checking for input on id=%lu\n",
1393 s, c->c_connid, 0 );
1396 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1397 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1399 Debug( LDAP_DEBUG_TRACE,
1400 "connection_read(%d): TLS accept failure "
1401 "error=%d id=%lu, closing\n",
1402 s, rc, c->c_connid );
1404 c->c_needs_tls_accept = 0;
1405 /* c_mutex is locked */
1406 connection_closing( c, "TLS negotiation failure" );
1407 connection_close( c );
1408 connection_return( c );
1411 } else if ( rc == 0 ) {
1413 struct berval authid = BER_BVNULL;
1416 c->c_needs_tls_accept = 0;
1418 /* we need to let SASL know */
1419 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1421 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1422 if( c->c_tls_ssf > c->c_ssf ) {
1423 c->c_ssf = c->c_tls_ssf;
1426 rc = dnX509peerNormalize( ssl, &authid );
1427 if ( rc != LDAP_SUCCESS ) {
1428 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1429 "unable to get TLS client DN, error=%d id=%lu\n",
1430 s, rc, c->c_connid );
1432 sprintf(msgbuf, "tls_ssf=%u ssf=%u", c->c_tls_ssf, c->c_ssf);
1433 Statslog( LDAP_DEBUG_STATS,
1434 "conn=%lu fd=%d TLS established %s tls_proto=%s tls_cipher=%s\n",
1435 c->c_connid, (int) s,
1436 msgbuf, ldap_pvt_tls_get_version( ssl ), ldap_pvt_tls_get_cipher( ssl ));
1437 slap_sasl_external( c, c->c_tls_ssf, &authid );
1438 if ( authid.bv_val ) free( authid.bv_val );
1441 struct berval cbv = { sizeof(cbinding), cbinding };
1442 if ( ldap_pvt_tls_get_unique( ssl, &cbv, 1 ))
1443 slap_sasl_cbinding( c, &cbv );
1445 } else if ( rc == 1 && ber_sockbuf_ctrl( c->c_sb,
1446 LBER_SB_OPT_NEEDS_WRITE, NULL )) { /* need to retry */
1447 slapd_set_write( s, 1 );
1448 connection_return( c );
1452 /* if success and data is ready, fall thru to data input loop */
1453 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1455 slapd_set_read( s, 1 );
1456 connection_return( c );
1462 #ifdef HAVE_CYRUS_SASL
1463 if ( c->c_sasl_layers ) {
1464 /* If previous layer is not removed yet, give up for now */
1465 if ( !c->c_sasl_sockctx ) {
1466 slapd_set_read( s, 1 );
1467 connection_return( c );
1471 c->c_sasl_layers = 0;
1473 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1474 if( rc != LDAP_SUCCESS ) {
1475 Debug( LDAP_DEBUG_TRACE,
1476 "connection_read(%d): SASL install error "
1477 "error=%d id=%lu, closing\n",
1478 s, rc, c->c_connid );
1480 /* c_mutex is locked */
1481 connection_closing( c, "SASL layer install failure" );
1482 connection_close( c );
1483 connection_return( c );
1489 #define CONNECTION_INPUT_LOOP 1
1490 /* #define DATA_READY_LOOP 1 */
1493 /* How do we do this without getting into a busy loop ? */
1494 rc = connection_input( c, cri );
1496 #ifdef DATA_READY_LOOP
1497 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1498 #elif defined CONNECTION_INPUT_LOOP
1505 Debug( LDAP_DEBUG_CONNS,
1506 "connection_read(%d): input error=%d id=%lu, closing.\n",
1507 s, rc, c->c_connid );
1509 /* c_mutex is locked */
1510 connection_closing( c, conn_lost_str );
1511 connection_close( c );
1512 connection_return( c );
1516 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1517 slapd_set_write( s, 0 );
1520 slapd_set_read( s, 1 );
1521 connection_return( c );
1527 connection_input( Connection *conn , conn_readinfo *cri )
1535 #ifdef LDAP_CONNECTIONLESS
1542 if ( conn->c_currentber == NULL &&
1543 ( conn->c_currentber = ber_alloc()) == NULL )
1545 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1551 #ifdef LDAP_CONNECTIONLESS
1552 if ( conn->c_is_udp ) {
1553 #if defined(LDAP_PF_INET6)
1554 char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
1555 char addr[INET6_ADDRSTRLEN];
1557 char peername[sizeof("IP=255.255.255.255:65336")];
1558 char addr[INET_ADDRSTRLEN];
1560 const char *peeraddr_string = NULL;
1562 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(Sockaddr));
1563 if (len != sizeof(Sockaddr)) return 1;
1565 #if defined(LDAP_PF_INET6)
1566 if (peeraddr.sa_addr.sa_family == AF_INET6) {
1567 if ( IN6_IS_ADDR_V4MAPPED(&peeraddr.sa_in6_addr.sin6_addr) ) {
1568 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1569 peeraddr_string = inet_ntop( AF_INET,
1570 ((struct in_addr *)&peeraddr.sa_in6_addr.sin6_addr.s6_addr[12]),
1571 addr, sizeof(addr) );
1572 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1573 peeraddr_string = inet_ntoa( *((struct in_addr *)
1574 &peeraddr.sa_in6_addr.sin6_addr.s6_addr[12]) );
1575 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1576 if ( !peeraddr_string ) peeraddr_string = SLAP_STRING_UNKNOWN;
1577 sprintf( peername, "IP=%s:%d", peeraddr_string,
1578 (unsigned) ntohs( peeraddr.sa_in6_addr.sin6_port ) );
1580 peeraddr_string = inet_ntop( AF_INET6,
1581 &peeraddr.sa_in6_addr.sin6_addr,
1582 addr, sizeof addr );
1583 if ( !peeraddr_string ) peeraddr_string = SLAP_STRING_UNKNOWN;
1584 sprintf( peername, "IP=[%s]:%d", peeraddr_string,
1585 (unsigned) ntohs( peeraddr.sa_in6_addr.sin6_port ) );
1589 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1591 peeraddr_string = inet_ntop( AF_INET, &peeraddr.sa_in_addr.sin_addr,
1592 addr, sizeof(addr) );
1593 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1594 peeraddr_string = inet_ntoa( peeraddr.sa_in_addr.sin_addr );
1595 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1596 sprintf( peername, "IP=%s:%d",
1598 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1600 Statslog( LDAP_DEBUG_STATS,
1601 "conn=%lu UDP request from %s (%s) accepted.\n",
1602 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1606 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1607 if ( tag != LDAP_TAG_MESSAGE ) {
1608 int err = sock_errno();
1610 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1611 /* log, close and send error */
1612 Debug( LDAP_DEBUG_TRACE,
1613 "ber_get_next on fd %d failed errno=%d (%s)\n",
1614 conn->c_sd, err, sock_errstr(err) );
1615 ber_free( conn->c_currentber, 1 );
1616 conn->c_currentber = NULL;
1623 ber = conn->c_currentber;
1624 conn->c_currentber = NULL;
1626 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1627 /* log, close and send error */
1628 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1633 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1634 /* log, close and send error */
1635 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1641 #ifdef LDAP_CONNECTIONLESS
1642 if( conn->c_is_udp ) {
1643 if( tag == LBER_OCTETSTRING ) {
1644 if ( (tag = ber_get_stringa( ber, &cdn )) != LBER_ERROR )
1645 tag = ber_peek_tag( ber, &len );
1647 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1648 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1655 if(tag == LDAP_REQ_BIND) {
1656 /* immediately abandon all existing operations upon BIND */
1657 connection_abandon( conn );
1661 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++, ctx );
1663 Debug( LDAP_DEBUG_TRACE, "op tag 0x%lx, time %ld\n", tag,
1664 (long) op->o_time, 0);
1667 /* clear state if the connection is being reused from inactive */
1668 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1669 memset( &conn->c_pagedresults_state, 0,
1670 sizeof( conn->c_pagedresults_state ) );
1673 op->o_res_ber = NULL;
1675 #ifdef LDAP_CONNECTIONLESS
1676 if (conn->c_is_udp) {
1678 ber_str2bv( cdn, 0, 1, &op->o_dn );
1679 op->o_protocol = LDAP_VERSION2;
1681 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1682 if (op->o_res_ber == NULL) return 1;
1684 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1685 sizeof(struct sockaddr), 0 );
1687 if (rc != sizeof(struct sockaddr)) {
1688 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1692 if (op->o_protocol == LDAP_VERSION2) {
1693 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1695 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1700 #endif /* LDAP_CONNECTIONLESS */
1704 /* Don't process requests when the conn is in the middle of a
1705 * Bind, or if it's closing. Also, don't let any single conn
1706 * use up all the available threads, and don't execute if we're
1707 * currently blocked on output. And don't execute if there are
1708 * already pending ops, let them go first. Abandon operations
1709 * get exceptions to some, but not all, cases.
1713 /* Abandon and Unbind are exempt from these checks */
1714 if (conn->c_conn_state == SLAP_C_CLOSING) {
1717 } else if (conn->c_writewaiter) {
1718 defer = "awaiting write";
1720 } else if (conn->c_n_ops_pending) {
1721 defer = "pending operations";
1725 case LDAP_REQ_ABANDON:
1726 /* Unbind is exempt from these checks */
1727 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1728 defer = "too many executing";
1730 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1735 case LDAP_REQ_UNBIND:
1740 int max = conn->c_dn.bv_len
1741 ? slap_conn_max_pending_auth
1742 : slap_conn_max_pending;
1744 Debug( LDAP_DEBUG_ANY,
1745 "connection_input: conn=%lu deferring operation: %s\n",
1746 conn->c_connid, defer, 0 );
1747 conn->c_n_ops_pending++;
1748 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1749 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1752 conn->c_n_ops_executing++;
1755 * The first op will be processed in the same thread context,
1756 * as long as there is only one op total.
1757 * Subsequent ops will be submitted to the pool by
1758 * calling connection_op_activate()
1760 if ( cri->op == NULL ) {
1761 /* the first incoming request */
1762 connection_op_queue( op );
1765 if ( !cri->nullop ) {
1767 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1768 connection_operation, (void *) cri->op );
1770 connection_op_activate( op );
1775 if ( conn->c_struct_state != SLAP_C_USED ) {
1776 /* connection must have got closed underneath us */
1781 assert( conn->c_struct_state == SLAP_C_USED );
1786 connection_resched( Connection *conn )
1790 if( conn->c_writewaiter )
1793 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1794 Debug( LDAP_DEBUG_CONNS, "connection_resched: "
1795 "attempting closing conn=%lu sd=%d\n",
1796 conn->c_connid, conn->c_sd, 0 );
1797 connection_close( conn );
1801 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1802 /* other states need different handling */
1806 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1807 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1809 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1810 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1812 /* pending operations should not be marked for abandonment */
1813 assert(!op->o_abandon);
1815 conn->c_n_ops_pending--;
1816 conn->c_n_ops_executing++;
1818 connection_op_activate( op );
1820 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1826 connection_init_log_prefix( Operation *op )
1828 if ( op->o_connid == (unsigned long)(-1) ) {
1829 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1830 "conn=-1 op=%lu", op->o_opid );
1833 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1834 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1838 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1840 op->o_conn->c_sasl_bindop = NULL;
1842 ch_free( op->o_callback );
1843 op->o_callback = NULL;
1845 return SLAP_CB_CONTINUE;
1848 static int connection_bind_cb( Operation *op, SlapReply *rs )
1850 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1851 op->o_conn->c_sasl_bind_in_progress =
1852 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1854 /* Moved here from bind.c due to ITS#4158 */
1855 op->o_conn->c_sasl_bindop = NULL;
1856 if ( op->orb_method == LDAP_AUTH_SASL ) {
1857 if( rs->sr_err == LDAP_SUCCESS ) {
1858 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1859 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1860 /* edn is always normalized already */
1861 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1863 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1864 BER_BVZERO( &op->orb_edn );
1865 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1866 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1868 op->o_conn->c_sasl_ssf = op->orb_ssf;
1869 if( op->orb_ssf > op->o_conn->c_ssf ) {
1870 op->o_conn->c_ssf = op->orb_ssf;
1873 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1874 ber_len_t max = sockbuf_max_incoming_auth;
1875 ber_sockbuf_ctrl( op->o_conn->c_sb,
1876 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1879 /* log authorization identity */
1880 Statslog( LDAP_DEBUG_STATS,
1881 "%s BIND dn=\"%s\" mech=%s sasl_ssf=%d ssf=%d\n",
1883 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1884 op->o_conn->c_authmech.bv_val,
1885 op->orb_ssf, op->o_conn->c_ssf );
1887 Debug( LDAP_DEBUG_TRACE,
1888 "do_bind: SASL/%s bind: dn=\"%s\" sasl_ssf=%d\n",
1889 op->o_conn->c_authmech.bv_val,
1890 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1893 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1894 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1895 free( op->o_conn->c_sasl_bind_mech.bv_val );
1896 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1900 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1902 ch_free( op->o_callback );
1903 op->o_callback = NULL;
1905 return SLAP_CB_CONTINUE;
1908 static void connection_op_queue( Operation *op )
1910 ber_tag_t tag = op->o_tag;
1912 if (tag == LDAP_REQ_BIND) {
1913 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1914 sc->sc_response = connection_bind_cb;
1915 sc->sc_cleanup = connection_bind_cleanup_cb;
1916 sc->sc_next = op->o_callback;
1917 op->o_callback = sc;
1918 op->o_conn->c_conn_state = SLAP_C_BINDING;
1921 if (!op->o_dn.bv_len) {
1922 op->o_authz = op->o_conn->c_authz;
1923 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1924 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1925 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1927 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1928 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1932 op->o_authtype = op->o_conn->c_authtype;
1933 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1935 if (!op->o_protocol) {
1936 op->o_protocol = op->o_conn->c_protocol
1937 ? op->o_conn->c_protocol : LDAP_VERSION3;
1940 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1941 op->o_protocol > LDAP_VERSION2)
1943 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1946 op->o_connid = op->o_conn->c_connid;
1947 connection_init_log_prefix( op );
1949 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1952 static int connection_op_activate( Operation *op )
1956 connection_op_queue( op );
1958 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1959 connection_operation, (void *) op );
1962 Debug( LDAP_DEBUG_ANY,
1963 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1964 rc, op->o_connid, 0 );
1965 /* should move op to pending list */
1971 int connection_write(ber_socket_t s)
1977 assert( connections != NULL );
1979 c = connection_get( s );
1981 Debug( LDAP_DEBUG_ANY,
1982 "connection_write(%ld): no connection!\n",
1987 slapd_clr_write( s, 0 );
1990 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1991 connection_return( c );
1992 connection_read_activate( s );
1999 Debug( LDAP_DEBUG_TRACE,
2000 "connection_write(%d): waking output for id=%lu\n",
2001 s, c->c_connid, 0 );
2003 wantwrite = ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL );
2004 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL )) {
2005 /* don't wakeup twice */
2006 slapd_set_read( s, !wantwrite );
2009 slapd_set_write( s, 1 );
2012 /* If there are ops pending because of a writewaiter,
2015 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
2016 if ( !c->c_writewaiter ) break;
2017 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
2019 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
2020 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2022 /* pending operations should not be marked for abandonment */
2023 assert(!op->o_abandon);
2025 c->c_n_ops_pending--;
2026 c->c_n_ops_executing++;
2028 connection_op_activate( op );
2033 connection_return( c );
2038 typedef struct conn_fake_extblock {
2041 } conn_fake_extblock;
2044 connection_fake_destroy(
2048 Connection conn = {0};
2050 Opheader ohdr = {0};
2052 conn_fake_extblock *eb = data;
2055 op.o_hdr->oh_extensions = eb->eb_op;
2056 conn.c_extensions = eb->eb_conn;
2061 ber_memfree_x( eb, NULL );
2062 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
2063 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
2068 connection_fake_init(
2070 OperationBuffer *opbuf,
2073 connection_fake_init2( conn, opbuf, ctx, 1 );
2077 operation_fake_init(
2083 /* set memory context */
2084 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
2086 op->o_tmpmfuncs = &slap_sl_mfuncs;
2087 op->o_threadctx = ctx;
2088 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
2090 op->o_counters = &slap_counters;
2092 op->o_connid = op->o_conn->c_connid;
2093 connection_init_log_prefix( op );
2098 connection_fake_init2(
2100 OperationBuffer *opbuf,
2104 Operation *op = (Operation *) opbuf;
2106 conn->c_connid = -1;
2107 conn->c_conn_idx = -1;
2108 conn->c_send_ldap_result = slap_send_ldap_result;
2109 conn->c_send_search_entry = slap_send_search_entry;
2110 conn->c_send_search_reference = slap_send_search_reference;
2111 conn->c_send_ldap_extended = slap_send_ldap_extended;
2112 conn->c_send_ldap_intermediate = slap_send_ldap_intermediate;
2113 conn->c_listener = (Listener *)&dummy_list;
2114 conn->c_peer_domain = slap_empty_bv;
2115 conn->c_peer_name = slap_empty_bv;
2117 memset( opbuf, 0, sizeof( *opbuf ));
2118 op->o_hdr = &opbuf->ob_hdr;
2119 op->o_controls = opbuf->ob_controls;
2121 operation_fake_init( conn, op, ctx, newmem );
2124 if ( slapi_plugins_used ) {
2125 conn_fake_extblock *eb;
2128 /* Use thread keys to make sure these eventually get cleaned up */
2129 if ( ldap_pvt_thread_pool_getkey( ctx, (void *)connection_fake_init,
2131 eb = ch_malloc( sizeof( *eb ));
2132 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2133 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2134 eb->eb_conn = conn->c_extensions;
2135 eb->eb_op = op->o_hdr->oh_extensions;
2136 ldap_pvt_thread_pool_setkey( ctx, (void *)connection_fake_init,
2137 eb, connection_fake_destroy, NULL, NULL );
2140 conn->c_extensions = eb->eb_conn;
2141 op->o_hdr->oh_extensions = eb->eb_op;
2144 #endif /* LDAP_SLAPI */
2146 slap_op_time( &op->o_time, &op->o_tincr );
2150 connection_assign_nextid( Connection *conn )
2152 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2153 conn->c_connid = conn_nextid++;
2154 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );