3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/string.h>
16 #include <ac/unistd.h>
26 /* protected by connections_mutex */
27 static ldap_pvt_thread_mutex_t connections_mutex;
28 static Connection *connections = NULL;
29 static unsigned long conn_nextid = 0;
31 /* structure state (protected by connections_mutex) */
32 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
33 #define SLAP_C_UNUSED 0x01
34 #define SLAP_C_USED 0x02
36 /* connection state (protected by c_mutex ) */
37 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
38 #define SLAP_C_INACTIVE 0x01 /* zero threads */
39 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
40 #define SLAP_C_BINDING 0x03 /* binding */
41 #define SLAP_C_CLOSING 0x04 /* closing */
44 connection_state2str( int state )
47 case SLAP_C_INVALID: return "!";
48 case SLAP_C_INACTIVE: return "|";
49 case SLAP_C_ACTIVE: return "";
50 case SLAP_C_BINDING: return "B";
51 case SLAP_C_CLOSING: return "C";
57 static Connection* connection_get( ber_socket_t s );
59 static int connection_input( Connection *c );
60 static void connection_close( Connection *c );
62 static int connection_op_activate( Operation *op );
63 static int connection_resched( Connection *conn );
64 static void connection_abandon( Connection *conn );
65 static void connection_destroy( Connection *c );
67 static ldap_pvt_thread_start_t connection_operation;
70 * Initialize connection management infrastructure.
72 int connections_init(void)
74 assert( connections == NULL );
76 if( connections != NULL) {
78 LDAP_LOG( CONNECTION, INFO,
79 "connections_init: already initialized.\n", 0, 0, 0 );
81 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
87 /* should check return of every call */
88 ldap_pvt_thread_mutex_init( &connections_mutex );
90 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
92 if( connections == NULL ) {
94 LDAP_LOG( CONNECTION, ERR,
95 "connections_init: allocation (%d * %ld) of connection "
96 "array failed\n", dtblsize, (long) sizeof(Connection), 0 );
98 Debug( LDAP_DEBUG_ANY,
99 "connections_init: allocation (%d*%ld) of connection array failed\n",
100 dtblsize, (long) sizeof(Connection), 0 );
105 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
106 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
109 * per entry initialization of the Connection array initialization
110 * will be done by connection_init()
117 * Destroy connection management infrastructure.
119 int connections_destroy(void)
123 /* should check return of every call */
125 if( connections == NULL) {
127 LDAP_LOG( CONNECTION, INFO,
128 "connections_destroy: nothing to destroy.\n", 0, 0, 0 );
130 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
136 for ( i = 0; i < dtblsize; i++ ) {
137 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
138 ber_sockbuf_free( connections[i].c_sb );
139 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
140 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
141 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
143 slapi_x_free_object_extensions( SLAPI_X_EXT_CONNECTION, &connections[i] );
151 ldap_pvt_thread_mutex_destroy( &connections_mutex );
156 * shutdown all connections
158 int connections_shutdown(void)
162 ldap_pvt_thread_mutex_lock( &connections_mutex );
164 for ( i = 0; i < dtblsize; i++ ) {
165 if( connections[i].c_struct_state != SLAP_C_USED ) {
169 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
171 /* connections_mutex and c_mutex are locked */
172 connection_closing( &connections[i] );
173 connection_close( &connections[i] );
175 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
178 ldap_pvt_thread_mutex_unlock( &connections_mutex );
184 * Timeout idle connections.
186 int connections_timeout_idle(time_t now)
192 for( c = connection_first( &connindex );
194 c = connection_next( c, &connindex ) )
196 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
198 connection_closing( c );
199 connection_close( c );
203 connection_done( c );
208 static Connection* connection_get( ber_socket_t s )
210 /* connections_mutex should be locked by caller */
215 LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
217 Debug( LDAP_DEBUG_ARGS,
218 "connection_get(%ld)\n",
222 assert( connections != NULL );
224 if(s == AC_SOCKET_INVALID) {
231 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
238 for(i=0; i<dtblsize; i++) {
239 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
240 assert( connections[i].c_conn_state == SLAP_C_INVALID );
241 assert( connections[i].c_sb == 0 );
245 ber_sockbuf_ctrl( connections[i].c_sb,
246 LBER_SB_OPT_GET_FD, &sd );
248 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
249 assert( connections[i].c_conn_state == SLAP_C_INVALID );
250 assert( sd == AC_SOCKET_INVALID );
254 /* state can actually change from used -> unused by resched,
255 * so don't assert details here.
269 ldap_pvt_thread_mutex_lock( &c->c_mutex );
271 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
272 if( c->c_struct_state != SLAP_C_USED ) {
273 /* connection must have been closed due to resched */
275 assert( c->c_conn_state == SLAP_C_INVALID );
276 assert( sd == AC_SOCKET_INVALID );
279 LDAP_LOG( CONNECTION, ARGS,
280 "connection_get: connection %d not used\n", s, 0, 0 );
282 Debug( LDAP_DEBUG_TRACE,
283 "connection_get(%d): connection not used\n",
287 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
292 LDAP_LOG( CONNECTION, RESULTS,
293 "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
295 Debug( LDAP_DEBUG_TRACE,
296 "connection_get(%d): got connid=%lu\n",
302 assert( c->c_struct_state == SLAP_C_USED );
303 assert( c->c_conn_state != SLAP_C_INVALID );
304 assert( sd != AC_SOCKET_INVALID );
307 c->c_activitytime = slap_get_time();
309 if( global_idletimeout > 0 ) {
310 c->c_activitytime = slap_get_time();
318 static void connection_return( Connection *c )
320 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
323 long connection_init(
327 const char* peername,
335 assert( connections != NULL );
337 assert( listener != NULL );
338 assert( dnsname != NULL );
339 assert( peername != NULL );
342 assert( tls_udp_option != 1 );
345 if( s == AC_SOCKET_INVALID ) {
347 LDAP_LOG( CONNECTION, INFO,
348 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
350 Debug( LDAP_DEBUG_ANY,
351 "connection_init(%ld): invalid.\n",
359 assert( s < dtblsize );
362 ldap_pvt_thread_mutex_lock( &connections_mutex );
373 for( i=0; i < dtblsize; i++) {
376 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
377 assert( connections[i].c_sb == 0 );
382 sd = AC_SOCKET_INVALID;
383 if (connections[i].c_sb != NULL)
384 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
386 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
387 assert( sd == AC_SOCKET_INVALID );
392 assert( connections[i].c_struct_state == SLAP_C_USED );
393 assert( connections[i].c_conn_state != SLAP_C_INVALID );
394 assert( sd != AC_SOCKET_INVALID );
399 LDAP_LOG( CONNECTION, INFO,
400 "connection_init: skt %d connection table full "
401 "(%d/%d)\n", s, i, dtblsize );
403 Debug( LDAP_DEBUG_ANY,
404 "connection_init(%d): connection table full "
405 "(%d/%d)\n", s, i, dtblsize);
407 ldap_pvt_thread_mutex_unlock( &connections_mutex );
415 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
416 c->c_send_ldap_result = slap_send_ldap_result;
417 c->c_send_search_entry = slap_send_search_entry;
418 c->c_send_search_reference = slap_send_search_reference;
419 c->c_send_ldap_extended = slap_send_ldap_extended;
420 #ifdef LDAP_RES_INTERMEDIATE
421 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
424 c->c_authmech.bv_val = NULL;
425 c->c_authmech.bv_len = 0;
426 c->c_dn.bv_val = NULL;
428 c->c_ndn.bv_val = NULL;
432 c->c_listener = NULL;
433 c->c_peer_domain.bv_val = NULL;
434 c->c_peer_domain.bv_len = 0;
435 c->c_peer_name.bv_val = NULL;
436 c->c_peer_name.bv_len = 0;
438 LDAP_STAILQ_INIT(&c->c_ops);
439 LDAP_STAILQ_INIT(&c->c_pending_ops);
441 c->c_sasl_bind_mech.bv_val = NULL;
442 c->c_sasl_bind_mech.bv_len = 0;
444 c->c_sasl_authctx = NULL;
445 c->c_sasl_sockctx = NULL;
446 c->c_sasl_extra = NULL;
447 c->c_sasl_bindop = NULL;
449 c->c_sb = ber_sockbuf_alloc( );
452 ber_len_t max = sockbuf_max_incoming;
453 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
456 c->c_currentber = NULL;
458 /* should check status of thread calls */
459 ldap_pvt_thread_mutex_init( &c->c_mutex );
460 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
461 ldap_pvt_thread_cond_init( &c->c_write_cv );
464 slapi_x_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
467 c->c_struct_state = SLAP_C_UNUSED;
470 ldap_pvt_thread_mutex_lock( &c->c_mutex );
472 assert( c->c_struct_state == SLAP_C_UNUSED );
473 assert( c->c_authmech.bv_val == NULL );
474 assert( c->c_dn.bv_val == NULL );
475 assert( c->c_ndn.bv_val == NULL );
476 assert( c->c_groups == NULL );
477 assert( c->c_listener == NULL );
478 assert( c->c_peer_domain.bv_val == NULL );
479 assert( c->c_peer_name.bv_val == NULL );
480 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
481 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
482 assert( c->c_sasl_bind_mech.bv_val == NULL );
483 assert( c->c_sasl_done == 0 );
484 assert( c->c_sasl_authctx == NULL );
485 assert( c->c_sasl_sockctx == NULL );
486 assert( c->c_sasl_extra == NULL );
487 assert( c->c_sasl_bindop == NULL );
488 assert( c->c_currentber == NULL );
490 c->c_listener = listener;
491 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
492 ber_str2bv( peername, 0, 1, &c->c_peer_name );
494 c->c_n_ops_received = 0;
495 c->c_n_ops_executing = 0;
496 c->c_n_ops_pending = 0;
497 c->c_n_ops_completed = 0;
503 /* set to zero until bind, implies LDAP_VERSION3 */
507 c->c_activitytime = c->c_starttime = slap_get_time();
509 if( global_idletimeout > 0 ) {
510 c->c_activitytime = c->c_starttime = slap_get_time();
514 #ifdef LDAP_CONNECTIONLESS
516 if( tls_udp_option == 2 ) {
519 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
520 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
522 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
523 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
524 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
525 LBER_SBIOD_LEVEL_PROVIDER, NULL );
530 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
531 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
533 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
534 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
538 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
539 INT_MAX, (void*)"ldap_" );
542 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
543 c /* non-NULL */ ) < 0 )
546 LDAP_LOG( CONNECTION, INFO,
547 "connection_init: conn %lu set nonblocking failed\n",
550 Debug( LDAP_DEBUG_ANY,
551 "connection_init(%d, %s): set nonblocking failed\n",
552 s, c->c_peer_name.bv_val, 0 );
556 id = c->c_connid = conn_nextid++;
558 c->c_conn_state = SLAP_C_INACTIVE;
559 c->c_struct_state = SLAP_C_USED;
561 c->c_ssf = c->c_transport_ssf = ssf;
565 if ( tls_udp_option == 1 ) {
567 c->c_needs_tls_accept = 1;
570 c->c_needs_tls_accept = 0;
574 slap_sasl_open( c, 0 );
575 slap_sasl_external( c, ssf, authid );
577 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
578 ldap_pvt_thread_mutex_unlock( &connections_mutex );
580 backend_connection_init(c);
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(c->c_authmech.bv_val != NULL ) {
596 free(c->c_authmech.bv_val);
597 c->c_authmech.bv_val = NULL;
599 c->c_authmech.bv_len = 0;
601 if(c->c_dn.bv_val != NULL) {
602 free(c->c_dn.bv_val);
603 c->c_dn.bv_val = NULL;
606 if(c->c_ndn.bv_val != NULL) {
607 free(c->c_ndn.bv_val);
608 c->c_ndn.bv_val = NULL;
612 c->c_authz_backend = NULL;
615 GroupAssertion *g, *n;
616 for (g = c->c_groups; g; g=n) {
625 connection_destroy( Connection *c )
627 /* note: connections_mutex should be locked by caller */
629 unsigned long connid;
631 assert( connections != NULL );
633 assert( c->c_struct_state != SLAP_C_UNUSED );
634 assert( c->c_conn_state != SLAP_C_INVALID );
635 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
637 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
638 connid = c->c_connid;
640 backend_connection_destroy(c);
645 c->c_activitytime = c->c_starttime = 0;
647 connection2anonymous( c );
648 c->c_listener = NULL;
650 if(c->c_peer_domain.bv_val != NULL) {
651 free(c->c_peer_domain.bv_val);
652 c->c_peer_domain.bv_val = NULL;
654 c->c_peer_domain.bv_len = 0;
655 if(c->c_peer_name.bv_val != NULL) {
656 free(c->c_peer_name.bv_val);
657 c->c_peer_name.bv_val = NULL;
659 c->c_peer_name.bv_len = 0;
661 c->c_sasl_bind_in_progress = 0;
662 if(c->c_sasl_bind_mech.bv_val != NULL) {
663 free(c->c_sasl_bind_mech.bv_val);
664 c->c_sasl_bind_mech.bv_val = NULL;
666 c->c_sasl_bind_mech.bv_len = 0;
668 slap_sasl_close( c );
670 if ( c->c_currentber != NULL ) {
671 ber_free( c->c_currentber, 1 );
672 c->c_currentber = NULL;
675 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
676 if ( sd != AC_SOCKET_INVALID ) {
677 slapd_remove( sd, 0 );
679 Statslog( LDAP_DEBUG_STATS,
680 "conn=%lu fd=%ld closed\n",
681 connid, (long) sd, 0, 0, 0 );
684 ber_sockbuf_free( c->c_sb );
686 c->c_sb = ber_sockbuf_alloc( );
689 ber_len_t max = sockbuf_max_incoming;
690 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
693 c->c_conn_state = SLAP_C_INVALID;
694 c->c_struct_state = SLAP_C_UNUSED;
697 /* call destructors, then constructors; avoids unnecessary allocation */
698 slapi_x_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
702 int connection_state_closing( Connection *c )
704 /* c_mutex must be locked by caller */
708 assert( c->c_struct_state == SLAP_C_USED );
710 state = c->c_conn_state;
712 assert( state != SLAP_C_INVALID );
714 return state == SLAP_C_CLOSING;
717 static void connection_abandon( Connection *c )
719 /* c_mutex must be locked by caller */
723 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
727 /* remove pending operations */
728 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
729 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
730 LDAP_STAILQ_NEXT(o, o_next) = NULL;
735 void connection_closing( Connection *c )
737 assert( connections != NULL );
739 assert( c->c_struct_state == SLAP_C_USED );
740 assert( c->c_conn_state != SLAP_C_INVALID );
742 /* c_mutex must be locked by caller */
744 if( c->c_conn_state != SLAP_C_CLOSING ) {
747 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
749 LDAP_LOG( CONNECTION, DETAIL1,
750 "connection_closing: conn %lu readying socket %d for close.\n",
751 c->c_connid, sd, 0 );
753 Debug( LDAP_DEBUG_TRACE,
754 "connection_closing: readying conn=%lu sd=%d for close\n",
755 c->c_connid, sd, 0 );
757 /* update state to closing */
758 c->c_conn_state = SLAP_C_CLOSING;
760 /* don't listen on this port anymore */
761 slapd_clr_read( sd, 1 );
763 /* abandon active operations */
764 connection_abandon( c );
766 /* wake write blocked operations */
767 slapd_clr_write( sd, 1 );
768 ldap_pvt_thread_cond_signal( &c->c_write_cv );
772 static void connection_close( Connection *c )
776 assert( connections != NULL );
778 assert( c->c_struct_state == SLAP_C_USED );
779 assert( c->c_conn_state == SLAP_C_CLOSING );
781 /* note: connections_mutex and c_mutex should be locked by caller */
783 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
784 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
786 LDAP_LOG( CONNECTION, DETAIL1,
787 "connection_close: conn %lu deferring sd %d\n",
788 c->c_connid, sd, 0 );
790 Debug( LDAP_DEBUG_TRACE,
791 "connection_close: deferring conn=%lu sd=%d\n",
792 c->c_connid, sd, 0 );
798 LDAP_LOG( CONNECTION, RESULTS,
799 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
801 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
802 c->c_connid, sd, 0 );
804 connection_destroy( c );
807 unsigned long connections_nextid(void)
810 assert( connections != NULL );
812 ldap_pvt_thread_mutex_lock( &connections_mutex );
816 ldap_pvt_thread_mutex_unlock( &connections_mutex );
821 Connection* connection_first( ber_socket_t *index )
823 assert( connections != NULL );
824 assert( index != NULL );
826 ldap_pvt_thread_mutex_lock( &connections_mutex );
830 return connection_next(NULL, index);
833 Connection* connection_next( Connection *c, ber_socket_t *index )
835 assert( connections != NULL );
836 assert( index != NULL );
837 assert( *index <= dtblsize );
840 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
845 for(; *index < dtblsize; (*index)++) {
846 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
847 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
855 if( connections[*index].c_struct_state == SLAP_C_USED ) {
856 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
857 c = &connections[(*index)++];
861 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
862 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
866 ldap_pvt_thread_mutex_lock( &c->c_mutex );
872 void connection_done( Connection *c )
874 assert( connections != NULL );
877 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
880 ldap_pvt_thread_mutex_unlock( &connections_mutex );
884 * connection_activity - handle the request operation op on connection
885 * conn. This routine figures out what kind of operation it is and
886 * calls the appropriate stub to handle it.
890 #define INCR_OP(var,index) \
892 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
894 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
896 #else /* !SLAPD_MONITOR */
897 #define INCR_OP(var,index)
898 #endif /* !SLAPD_MONITOR */
901 connection_operation( void *ctx, void *arg_v )
903 int rc = SLAPD_DISCONNECT;
904 Operation *op = arg_v;
905 SlapReply rs = {REP_RESULT};
906 ber_tag_t tag = op->o_tag;
908 ber_tag_t oldtag = tag;
909 #endif /* SLAPD_MONITOR */
910 Connection *conn = op->o_conn;
914 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
916 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
918 op->o_threadctx = ctx;
920 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
922 LDAP_LOG( CONNECTION, ERR,
923 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
924 conn->c_connid, (long)tag, 0 );
926 Debug( LDAP_DEBUG_ANY, "connection_operation: "
927 "error: SASL bind in progress (tag=%ld).\n",
930 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
931 "SASL bind in progress" );
932 goto operations_error;
935 /* We can use Thread-Local storage for most mallocs. We can
936 * also use TL for ber parsing, but not on Add or Modify.
938 #define SLAB_SIZE 1048576
940 memsiz = ber_len( op->o_ber ) * 64;
941 if ( SLAB_SIZE > memsiz ) memsiz = SLAB_SIZE;
945 memctx = sl_mem_create( memsiz, ctx );
946 op->o_tmpmemctx = memctx;
947 op->o_tmpmfuncs = &sl_mfuncs;
948 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
949 /* Note - the ber and its buffer are already allocated from
950 * regular memory; this only affects subsequent mallocs that
951 * ber_scanf may invoke.
953 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
958 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
959 rc = do_bind( op, &rs );
962 case LDAP_REQ_UNBIND:
963 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
964 rc = do_unbind( op, &rs );
968 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
969 rc = do_add( op, &rs );
972 case LDAP_REQ_DELETE:
973 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
974 rc = do_delete( op, &rs );
977 case LDAP_REQ_MODRDN:
978 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
979 rc = do_modrdn( op, &rs );
982 case LDAP_REQ_MODIFY:
983 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
984 rc = do_modify( op, &rs );
987 case LDAP_REQ_COMPARE:
988 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
989 rc = do_compare( op, &rs );
992 case LDAP_REQ_SEARCH:
993 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
994 rc = do_search( op, &rs );
997 case LDAP_REQ_ABANDON:
998 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
999 rc = do_abandon( op, &rs );
1002 case LDAP_REQ_EXTENDED:
1003 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
1004 rc = do_extended( op, &rs );
1009 LDAP_LOG( CONNECTION, INFO,
1010 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
1011 conn->c_connid, tag, 0 );
1013 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
1016 op->o_tag = LBER_ERROR;
1017 rs.sr_err = LDAP_PROTOCOL_ERROR;
1018 rs.sr_text = "unknown LDAP request";
1019 send_ldap_disconnect( op, &rs );
1024 #ifdef SLAPD_MONITOR
1026 #endif /* SLAPD_MONITOR */
1027 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1030 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1031 num_ops_completed++;
1032 #ifdef SLAPD_MONITOR
1035 num_ops_completed_[SLAP_OP_BIND]++;
1037 case LDAP_REQ_UNBIND:
1038 num_ops_completed_[SLAP_OP_UNBIND]++;
1041 num_ops_completed_[SLAP_OP_ADD]++;
1043 case LDAP_REQ_DELETE:
1044 num_ops_completed_[SLAP_OP_DELETE]++;
1046 case LDAP_REQ_MODRDN:
1047 num_ops_completed_[SLAP_OP_MODRDN]++;
1049 case LDAP_REQ_MODIFY:
1050 num_ops_completed_[SLAP_OP_MODIFY]++;
1052 case LDAP_REQ_COMPARE:
1053 num_ops_completed_[SLAP_OP_COMPARE]++;
1055 case LDAP_REQ_SEARCH:
1056 num_ops_completed_[SLAP_OP_SEARCH]++;
1058 case LDAP_REQ_ABANDON:
1059 num_ops_completed_[SLAP_OP_ABANDON]++;
1061 case LDAP_REQ_EXTENDED:
1062 num_ops_completed_[SLAP_OP_EXTENDED]++;
1065 #endif /* SLAPD_MONITOR */
1066 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1068 #ifdef LDAP_EXOP_X_CANCEL
1069 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1070 op->o_cancel = LDAP_TOO_LATE;
1073 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1074 op->o_cancel != SLAP_CANCEL_DONE )
1076 ldap_pvt_thread_yield();
1080 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1082 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1083 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1085 if ( op->o_cancel == SLAP_CANCEL_ACK )
1087 if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) {
1088 sl_mem_detach( ctx, memctx );
1094 conn->c_n_ops_executing--;
1095 conn->c_n_ops_completed++;
1097 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1104 case LDAP_REQ_UNBIND:
1105 /* c_mutex is locked */
1106 connection_closing( conn );
1110 conn->c_sasl_bind_in_progress =
1111 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1113 if( conn->c_conn_state == SLAP_C_BINDING) {
1114 conn->c_conn_state = SLAP_C_ACTIVE;
1118 connection_resched( conn );
1120 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1125 int connection_read(ber_socket_t s)
1130 assert( connections != NULL );
1132 ldap_pvt_thread_mutex_lock( &connections_mutex );
1134 /* get (locked) connection */
1135 c = connection_get( s );
1139 LDAP_LOG( CONNECTION, INFO,
1140 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1142 Debug( LDAP_DEBUG_ANY,
1143 "connection_read(%ld): no connection!\n",
1148 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1154 if( c->c_conn_state == SLAP_C_CLOSING ) {
1156 LDAP_LOG( CONNECTION, INFO,
1157 "connection_read: conn %lu connection closing, ignoring input\n",
1158 c->c_connid, 0, 0 );
1160 Debug( LDAP_DEBUG_TRACE,
1161 "connection_read(%d): closing, ignoring input for id=%lu\n",
1162 s, c->c_connid, 0 );
1164 connection_return( c );
1165 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1170 LDAP_LOG( CONNECTION, DETAIL1,
1171 "connection_read: conn %lu checking for input.\n",
1172 c->c_connid, 0, 0 );
1174 Debug( LDAP_DEBUG_TRACE,
1175 "connection_read(%d): checking for input on id=%lu\n",
1176 s, c->c_connid, 0 );
1180 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1181 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1183 #if 0 /* required by next #if 0 */
1189 LDAP_LOG( CONNECTION, ERR,
1190 "connection_read: conn %lu TLS accept error, error %d\n",
1191 c->c_connid, rc, 0 );
1193 Debug( LDAP_DEBUG_TRACE,
1194 "connection_read(%d): TLS accept error "
1195 "error=%d id=%lu, closing\n",
1196 s, rc, c->c_connid );
1198 c->c_needs_tls_accept = 0;
1199 /* connections_mutex and c_mutex are locked */
1200 connection_closing( c );
1203 /* Drain input before close, to allow SSL error codes
1204 * to propagate to client. */
1210 rc = select(s+1, &rfd, NULL, NULL, &tv);
1212 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1216 connection_close( c );
1218 } else if ( rc == 0 ) {
1220 struct berval authid = { 0, NULL };
1222 c->c_needs_tls_accept = 0;
1224 /* we need to let SASL know */
1225 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1227 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1228 if( c->c_tls_ssf > c->c_ssf ) {
1229 c->c_ssf = c->c_tls_ssf;
1232 rc = dnX509peerNormalize( ssl, &authid );
1233 if ( rc != LDAP_SUCCESS ) {
1235 LDAP_LOG( CONNECTION, INFO,
1236 "connection_read: conn %lu unable to get TLS client DN, "
1237 "error %d\n", c->c_connid, rc, 0 );
1239 Debug( LDAP_DEBUG_TRACE,
1240 "connection_read(%d): unable to get TLS client DN "
1241 "error=%d id=%lu\n",
1242 s, rc, c->c_connid );
1245 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1246 if ( authid.bv_val ) free( authid.bv_val );
1249 /* if success and data is ready, fall thru to data input loop */
1251 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1253 connection_return( c );
1254 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1260 #ifdef HAVE_CYRUS_SASL
1261 if ( c->c_sasl_layers ) {
1262 /* If previous layer is not removed yet, give up for now */
1263 if ( !c->c_sasl_sockctx ) {
1264 connection_return( c );
1265 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1269 c->c_sasl_layers = 0;
1271 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1273 if( rc != LDAP_SUCCESS ) {
1275 LDAP_LOG( CONNECTION, ERR,
1276 "connection_read: conn %lu SASL install error %d, closing\n",
1277 c->c_connid, rc, 0 );
1279 Debug( LDAP_DEBUG_TRACE,
1280 "connection_read(%d): SASL install error "
1281 "error=%d id=%lu, closing\n",
1282 s, rc, c->c_connid );
1284 /* connections_mutex and c_mutex are locked */
1285 connection_closing( c );
1286 connection_close( c );
1287 connection_return( c );
1288 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1294 #define CONNECTION_INPUT_LOOP 1
1295 /* #define DATA_READY_LOOP 1 */
1299 /* How do we do this without getting into a busy loop ? */
1300 rc = connection_input( c );
1302 #ifdef DATA_READY_LOOP
1303 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) );
1304 #elif CONNECTION_INPUT_LOOP
1312 LDAP_LOG( CONNECTION, ERR,
1313 "connection_read: conn %lu input error %d, closing.\n",
1314 c->c_connid, rc, 0 );
1316 Debug( LDAP_DEBUG_TRACE,
1317 "connection_read(%d): input error=%d id=%lu, closing.\n",
1318 s, rc, c->c_connid );
1320 /* connections_mutex and c_mutex are locked */
1321 connection_closing( c );
1322 connection_close( c );
1323 connection_return( c );
1324 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1328 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1329 slapd_set_read( s, 1 );
1332 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1333 slapd_set_write( s, 1 );
1336 connection_return( c );
1337 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1352 #ifdef LDAP_CONNECTIONLESS
1357 if ( conn->c_currentber == NULL &&
1358 ( conn->c_currentber = ber_alloc()) == NULL )
1361 LDAP_LOG( CONNECTION, ERR,
1362 "connection_input: conn %lu ber_alloc failed.\n",
1363 conn->c_connid, 0, 0 );
1365 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1372 #ifdef LDAP_CONNECTIONLESS
1373 if ( conn->c_is_udp ) {
1374 char peername[sizeof("IP=255.255.255.255:65336")];
1375 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1376 sizeof(struct sockaddr));
1377 if (len != sizeof(struct sockaddr))
1379 sprintf( peername, "IP=%s:%d",
1380 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1381 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1382 Statslog( LDAP_DEBUG_STATS,
1383 "conn=%lu UDP request from %s (%s) accepted.\n",
1384 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1387 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1388 if ( tag != LDAP_TAG_MESSAGE ) {
1392 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1395 LDAP_LOG( CONNECTION, ERR,
1396 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1397 conn->c_connid, err, sock_errstr(err) );
1399 Debug( LDAP_DEBUG_TRACE,
1400 "ber_get_next on fd %d failed errno=%d (%s)\n",
1401 sd, err, sock_errstr(err) );
1403 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1404 /* log, close and send error */
1405 ber_free( conn->c_currentber, 1 );
1406 conn->c_currentber = NULL;
1413 ber = conn->c_currentber;
1414 conn->c_currentber = NULL;
1416 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1417 /* log, close and send error */
1419 LDAP_LOG( CONNECTION, ERR,
1420 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1421 conn->c_connid, tag, 0 );
1423 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1430 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1431 /* log, close and send error */
1433 LDAP_LOG( CONNECTION, ERR,
1434 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1435 conn->c_connid, tag, 0 );
1437 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1445 #ifdef LDAP_CONNECTIONLESS
1446 if( conn->c_is_udp ) {
1447 if( tag == LBER_OCTETSTRING ) {
1448 ber_get_stringa( ber, &cdn );
1449 tag = ber_peek_tag(ber, &len);
1451 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1453 LDAP_LOG( CONNECTION, ERR,
1454 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1455 conn->c_connid, tag, 0 );
1457 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1465 if(tag == LDAP_REQ_BIND) {
1466 /* immediately abandon all exiting operations upon BIND */
1467 connection_abandon( conn );
1470 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1473 op->o_assertion = NULL;
1474 op->o_preread_attrs = NULL;
1475 op->o_postread_attrs = NULL;
1476 op->o_vrFilter = NULL;
1478 #ifdef LDAP_CONTROL_PAGEDRESULTS
1479 op->o_pagedresults_state = conn->c_pagedresults_state;
1482 op->o_res_ber = NULL;
1484 #ifdef LDAP_CONNECTIONLESS
1485 if (conn->c_is_udp) {
1487 ber_str2bv( cdn, 0, 1, &op->o_dn );
1488 op->o_protocol = LDAP_VERSION2;
1490 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1491 if (op->o_res_ber == NULL) return 1;
1493 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1494 sizeof(struct sockaddr), 0 );
1496 if (rc != sizeof(struct sockaddr)) {
1498 LDAP_LOG( CONNECTION, INFO,
1499 "connection_input: conn %lu ber_write failed\n",
1500 conn->c_connid, 0, 0 );
1502 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1507 if (op->o_protocol == LDAP_VERSION2) {
1508 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1511 LDAP_LOG( CONNECTION, INFO,
1512 "connection_input: conn %lu put outer sequence failed\n",
1513 conn->c_connid, 0, 0 );
1515 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1521 #endif /* LDAP_CONNECTIONLESS */
1525 /* Don't process requests when the conn is in the middle of a
1526 * Bind, or if it's closing. Also, don't let any single conn
1527 * use up all the available threads, and don't execute if we're
1528 * currently blocked on output. And don't execute if there are
1529 * already pending ops, let them go first.
1531 * But always allow Abandon through; it won't cost much.
1533 if ( tag != LDAP_REQ_ABANDON && (conn->c_conn_state == SLAP_C_BINDING
1534 || conn->c_conn_state == SLAP_C_CLOSING
1535 || conn->c_n_ops_executing >= connection_pool_max/2
1536 || conn->c_n_ops_pending
1537 || conn->c_writewaiter))
1539 int max = conn->c_dn.bv_len ? slap_conn_max_pending_auth
1540 : slap_conn_max_pending;
1542 LDAP_LOG( CONNECTION, INFO,
1543 "connection_input: conn %lu deferring operation\n",
1544 conn->c_connid, 0, 0 );
1546 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1548 conn->c_n_ops_pending++;
1549 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1550 if ( conn->c_n_ops_pending > max ) {
1556 conn->c_n_ops_executing++;
1557 connection_op_activate( op );
1561 if ( conn->c_struct_state != SLAP_C_USED ) {
1562 /* connection must have got closed underneath us */
1566 assert( conn->c_struct_state == SLAP_C_USED );
1572 connection_resched( Connection *conn )
1576 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1579 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1581 /* us trylock to avoid possible deadlock */
1582 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1586 LDAP_LOG( CONNECTION, DETAIL1,
1587 "connection_resched: conn %lu reaquiring locks.\n",
1588 conn->c_connid, 0, 0 );
1590 Debug( LDAP_DEBUG_TRACE,
1591 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1592 conn->c_connid, sd, 0 );
1595 * reaquire locks in the right order...
1596 * this may allow another thread to close this connection,
1597 * so recheck state below.
1599 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1600 ldap_pvt_thread_mutex_lock( &connections_mutex );
1601 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1604 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1606 LDAP_LOG( CONNECTION, INFO,
1607 "connection_resched: conn %lu closed by other thread.\n",
1608 conn->c_connid, 0, 0 );
1610 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1611 "closed by other thread conn=%lu sd=%d\n",
1612 conn->c_connid, sd, 0 );
1616 LDAP_LOG( CONNECTION, DETAIL1,
1617 "connection_resched: conn %lu attempting closing.\n",
1618 conn->c_connid, 0, 0 );
1620 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1621 "attempting closing conn=%lu sd=%d\n",
1622 conn->c_connid, sd, 0 );
1624 connection_close( conn );
1627 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1631 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1632 /* other states need different handling */
1636 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1637 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1640 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1641 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1642 /* pending operations should not be marked for abandonment */
1643 assert(!op->o_abandon);
1645 conn->c_n_ops_pending--;
1646 conn->c_n_ops_executing++;
1648 connection_op_activate( op );
1650 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1657 static int connection_op_activate( Operation *op )
1660 ber_tag_t tag = op->o_tag;
1662 if(tag == LDAP_REQ_BIND) {
1663 op->o_conn->c_conn_state = SLAP_C_BINDING;
1666 if (!op->o_dn.bv_len) {
1667 op->o_authz = op->o_conn->c_authz;
1668 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1669 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1671 op->o_authtype = op->o_conn->c_authtype;
1672 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1674 if (!op->o_protocol) {
1675 op->o_protocol = op->o_conn->c_protocol
1676 ? op->o_conn->c_protocol : LDAP_VERSION3;
1678 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1679 && op->o_protocol > LDAP_VERSION2) {
1680 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1683 op->o_connid = op->o_conn->c_connid;
1685 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1687 status = ldap_pvt_thread_pool_submit( &connection_pool,
1688 connection_operation, (void *) op );
1690 if ( status != 0 ) {
1692 LDAP_LOG( CONNECTION, ERR,
1693 "connection_op_activate: conn %lu thread pool submit failed.\n",
1694 op->o_connid, 0, 0 );
1696 Debug( LDAP_DEBUG_ANY,
1697 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1699 /* should move op to pending list */
1705 int connection_write(ber_socket_t s)
1709 assert( connections != NULL );
1711 ldap_pvt_thread_mutex_lock( &connections_mutex );
1713 c = connection_get( s );
1715 slapd_clr_write( s, 0);
1719 LDAP_LOG( CONNECTION, ERR,
1720 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1722 Debug( LDAP_DEBUG_ANY,
1723 "connection_write(%ld): no connection!\n",
1727 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1734 LDAP_LOG( CONNECTION, DETAIL1,
1735 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1737 Debug( LDAP_DEBUG_TRACE,
1738 "connection_write(%d): waking output for id=%lu\n",
1739 s, c->c_connid, 0 );
1741 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1743 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1744 slapd_set_read( s, 1 );
1745 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1746 slapd_set_write( s, 1 );
1747 connection_return( c );
1748 ldap_pvt_thread_mutex_unlock( &connections_mutex );