]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
ce4f7f08615d63f8f799380e58a2c7f6b4aa46ad
[openldap] / servers / slapd / connection.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10 #include <limits.h>
11
12 #include <ac/socket.h>
13 #include <ac/errno.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17 #include <ac/unistd.h>
18
19 #include "ldap_pvt.h"
20 #include "lutil.h"
21 #include "slap.h"
22
23 /* protected by connections_mutex */
24 static ldap_pvt_thread_mutex_t connections_mutex;
25 static Connection *connections = NULL;
26 static unsigned long conn_nextid = 0;
27
28 /* structure state (protected by connections_mutex) */
29 #define SLAP_C_UNINITIALIZED    0x00    /* MUST BE ZERO (0) */
30 #define SLAP_C_UNUSED                   0x01
31 #define SLAP_C_USED                             0x02
32
33 /* connection state (protected by c_mutex ) */
34 #define SLAP_C_INVALID                  0x00    /* MUST BE ZERO (0) */
35 #define SLAP_C_INACTIVE                 0x01    /* zero threads */
36 #define SLAP_C_ACTIVE                   0x02    /* one or more threads */
37 #define SLAP_C_BINDING                  0x03    /* binding */
38 #define SLAP_C_CLOSING                  0x04    /* closing */
39
40 const char *
41 connection_state2str( int state )
42 {
43         switch( state ) {
44         case SLAP_C_INVALID:    return "!";             
45         case SLAP_C_INACTIVE:   return "|";             
46         case SLAP_C_ACTIVE:             return "";                      
47         case SLAP_C_BINDING:    return "B";
48         case SLAP_C_CLOSING:    return "C";                     
49         }
50
51         return "?";
52 }
53
54 static Connection* connection_get( ber_socket_t s );
55
56 static int connection_input( Connection *c );
57 static void connection_close( Connection *c );
58
59 static int connection_op_activate( Connection *conn, Operation *op );
60 static int connection_resched( Connection *conn );
61 static void connection_abandon( Connection *conn );
62 static void connection_destroy( Connection *c );
63
64 struct co_arg {
65         Connection      *co_conn;
66         Operation       *co_op;
67 };
68
69 /*
70  * Initialize connection management infrastructure.
71  */
72 int connections_init(void)
73 {
74         assert( connections == NULL );
75
76         if( connections != NULL) {
77 #ifdef NEW_LOGGING
78                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
79                            "connections_init:  already initialized.\n" ));
80 #else
81                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
82                         0, 0, 0 );
83 #endif
84                 return -1;
85         }
86
87         /* should check return of every call */
88         ldap_pvt_thread_mutex_init( &connections_mutex );
89
90         connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
91
92         if( connections == NULL ) {
93 #ifdef NEW_LOGGING
94                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
95                            "connections_init: allocation (%d * %ld) of connection array failed\n",
96                            dtblsize, (long) sizeof(Connection) ));
97 #else
98                 Debug( LDAP_DEBUG_ANY,
99                         "connections_init: allocation (%d*%ld) of connection array failed\n",
100                         dtblsize, (long) sizeof(Connection), 0 );
101 #endif
102                 return -1;
103         }
104
105     assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
106     assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
107
108         /*
109          * per entry initialization of the Connection array initialization
110          * will be done by connection_init()
111          */ 
112
113         return 0;
114 }
115
116 /*
117  * Destroy connection management infrastructure.
118  */
119 int connections_destroy(void)
120 {
121         ber_socket_t i;
122
123         /* should check return of every call */
124
125         if( connections == NULL) {
126 #ifdef NEW_LOGGING
127                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
128                            "connections_destroy: nothing to destroy.\n"));
129 #else
130                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
131                         0, 0, 0 );
132 #endif
133                 return -1;
134         }
135
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 );
142                 }
143         }
144
145         free( connections );
146         connections = NULL;
147
148         ldap_pvt_thread_mutex_destroy( &connections_mutex );
149         return 0;
150 }
151
152 /*
153  * shutdown all connections
154  */
155 int connections_shutdown(void)
156 {
157         ber_socket_t i;
158
159         ldap_pvt_thread_mutex_lock( &connections_mutex );
160
161         for ( i = 0; i < dtblsize; i++ ) {
162                 if( connections[i].c_struct_state != SLAP_C_USED ) {
163                         continue;
164                 }
165
166                 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
167
168                 /* connections_mutex and c_mutex are locked */
169                 connection_closing( &connections[i] );
170                 connection_close( &connections[i] );
171
172                 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
173         }
174
175         ldap_pvt_thread_mutex_unlock( &connections_mutex );
176
177         return 0;
178 }
179
180 /*
181  * Timeout idle connections.
182  */
183 int connections_timeout_idle(time_t now)
184 {
185         int i = 0;
186         int connindex;
187         Connection* c;
188
189         for( c = connection_first( &connindex );
190                 c != NULL;
191                 c = connection_next( c, &connindex ) )
192         {
193                 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
194                         /* close it */
195                         connection_closing( c );
196                         connection_close( c );
197                         i++;
198                 }
199         }
200         connection_done( c );
201
202         return i;
203 }
204
205 static Connection* connection_get( ber_socket_t s )
206 {
207         /* connections_mutex should be locked by caller */
208
209         Connection *c;
210
211 #ifdef NEW_LOGGING
212         LDAP_LOG(( "connection", LDAP_LEVEL_ENTRY,
213                    "connection_get: socket %ld\n", (long)s ));
214 #else
215         Debug( LDAP_DEBUG_ARGS,
216                 "connection_get(%ld)\n",
217                 (long) s, 0, 0 );
218 #endif
219
220         assert( connections != NULL );
221
222         if(s == AC_SOCKET_INVALID) {
223                 return NULL;
224         }
225
226 #ifndef HAVE_WINSOCK
227         c = &connections[s];
228
229         assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
230
231 #else
232         c = NULL;
233         {
234                 ber_socket_t i, sd;
235
236                 for(i=0; i<dtblsize; i++) {
237                         ber_sockbuf_ctrl( connections[i].c_sb,
238                                 LBER_SB_OPT_GET_FD, &sd );
239
240                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
241                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
242                                 assert( connections[i].c_sb == 0 );
243                                 break;
244                         }
245
246                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
247                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
248                                 assert( sd == AC_SOCKET_INVALID );
249                                 continue;
250                         }
251
252                         /* state can actually change from used -> unused by resched,
253                          * so don't assert details here.
254                          */
255
256                         if( sd == s ) {
257                                 c = &connections[i];
258                                 break;
259                         }
260                 }
261         }
262 #endif
263
264         if( c != NULL ) {
265                 ber_socket_t    sd;
266
267                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
268
269                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
270                 if( c->c_struct_state != SLAP_C_USED ) {
271                         /* connection must have been closed due to resched */
272
273                         assert( c->c_conn_state == SLAP_C_INVALID );
274                         assert( sd == AC_SOCKET_INVALID );
275
276 #ifdef NEW_LOGGING
277                         LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
278                                    "connection_get:  connection %d not used\n", s ));
279 #else
280                         Debug( LDAP_DEBUG_TRACE,
281                                 "connection_get(%d): connection not used\n",
282                                 s, 0, 0 );
283 #endif
284
285                         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
286                         return NULL;
287                 }
288
289 #ifdef NEW_LOGGING
290                 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
291                            "connection_get: get for %d got connid %ld\n",s, c->c_connid ));
292 #else
293                 Debug( LDAP_DEBUG_TRACE,
294                         "connection_get(%d): got connid=%ld\n",
295                         s, c->c_connid, 0 );
296 #endif
297
298                 c->c_n_get++;
299
300                 assert( c->c_struct_state == SLAP_C_USED );
301                 assert( c->c_conn_state != SLAP_C_INVALID );
302                 assert( sd != AC_SOCKET_INVALID );
303
304 #ifdef SLAPD_MONITOR
305                 c->c_activitytime = slap_get_time();
306 #else
307                 if( global_idletimeout > 0 ) {
308                         c->c_activitytime = slap_get_time();
309                 }
310 #endif
311         }
312
313         return c;
314 }
315
316 static void connection_return( Connection *c )
317 {
318         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
319 }
320
321 long connection_init(
322         ber_socket_t s,
323         const char* url,
324         const char* dnsname,
325         const char* peername,
326         const char* sockname,
327         int tls_udp_option,
328         slap_ssf_t ssf,
329         const char *authid )
330 {
331         unsigned long id;
332         Connection *c;
333
334         assert( connections != NULL );
335
336         assert( dnsname != NULL );
337         assert( peername != NULL );
338         assert( sockname != NULL );
339
340 #ifndef HAVE_TLS
341         assert( tls_udp_option != 1 );
342 #endif
343
344         if( s == AC_SOCKET_INVALID ) {
345 #ifdef NEW_LOGGING
346                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
347                            "connection_init: init of socket %ld invalid.\n", (long)s ));
348 #else
349                 Debug( LDAP_DEBUG_ANY,
350                        "connection_init(%ld): invalid.\n",
351                        (long) s, 0, 0 );
352 #endif
353                 return -1;
354         }
355
356         assert( s >= 0 );
357 #ifndef HAVE_WINSOCK
358         assert( s < dtblsize );
359 #endif
360
361         ldap_pvt_thread_mutex_lock( &connections_mutex );
362
363 #ifndef HAVE_WINSOCK
364         c = &connections[s];
365
366 #else
367         {
368                 ber_socket_t    i;
369
370                 c = NULL;
371
372         for( i=0; i < dtblsize; i++) {
373                 ber_socket_t    sd;
374
375             if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
376                 assert( connections[i].c_sb == 0 );
377                 c = &connections[i];
378                 break;
379             }
380
381                         sd = AC_SOCKET_INVALID;
382                         if (connections[i].c_sb != NULL)
383                         ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
384             
385             if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
386                 assert( sd == AC_SOCKET_INVALID );
387                 c = &connections[i];
388                 break;
389             }
390
391             assert( connections[i].c_struct_state == SLAP_C_USED );
392             assert( connections[i].c_conn_state != SLAP_C_INVALID );
393             assert( sd != AC_SOCKET_INVALID );
394         }
395
396         if( c == NULL ) {
397 #ifdef NEW_LOGGING
398                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
399                            "connection_init: skt %d      connection table full (%d/%d)\n",
400                            s, i, dtblsize ));
401 #else
402                 Debug( LDAP_DEBUG_ANY,
403                                 "connection_init(%d): connection table full (%d/%d)\n",
404                                 s, i, dtblsize);
405 #endif
406             ldap_pvt_thread_mutex_unlock( &connections_mutex );
407             return -1;
408         }
409     }
410 #endif
411
412     assert( c != NULL );
413
414         if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
415                 c->c_authmech.bv_val = NULL;
416                 c->c_authmech.bv_len = 0;
417                 c->c_dn.bv_val = NULL;
418                 c->c_dn.bv_len = 0;
419                 c->c_ndn.bv_val = NULL;
420                 c->c_ndn.bv_len = 0;
421                 c->c_cdn.bv_val = NULL;
422                 c->c_cdn.bv_len = 0;
423                 c->c_groups = NULL;
424
425                 c->c_listener_url = NULL;
426                 c->c_peer_domain = NULL;
427                 c->c_peer_name = NULL;
428                 c->c_sock_name = NULL;
429
430                 LDAP_STAILQ_INIT(&c->c_ops);
431                 LDAP_STAILQ_INIT(&c->c_pending_ops);
432
433                 c->c_sasl_bind_mech.bv_val = NULL;
434                 c->c_sasl_bind_mech.bv_len = 0;
435                 c->c_sasl_context = NULL;
436                 c->c_sasl_extra = NULL;
437
438                 c->c_sb = ber_sockbuf_alloc( );
439
440                 {
441                         ber_len_t max = sockbuf_max_incoming;
442                         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
443                 }
444
445                 c->c_currentber = NULL;
446
447                 /* should check status of thread calls */
448                 ldap_pvt_thread_mutex_init( &c->c_mutex );
449                 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
450                 ldap_pvt_thread_cond_init( &c->c_write_cv );
451
452                 c->c_struct_state = SLAP_C_UNUSED;
453         }
454
455     ldap_pvt_thread_mutex_lock( &c->c_mutex );
456
457     assert( c->c_struct_state == SLAP_C_UNUSED );
458     assert( c->c_authmech.bv_val == NULL );
459     assert( c->c_dn.bv_val == NULL );
460     assert( c->c_ndn.bv_val == NULL );
461     assert( c->c_cdn.bv_val == NULL );
462     assert( c->c_groups == NULL );
463     assert( c->c_listener_url == NULL );
464     assert( c->c_peer_domain == NULL );
465     assert( c->c_peer_name == NULL );
466     assert( c->c_sock_name == NULL );
467     assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
468     assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
469         assert( c->c_sasl_bind_mech.bv_val == NULL );
470         assert( c->c_sasl_context == NULL );
471         assert( c->c_sasl_extra == NULL );
472         assert( c->c_currentber == NULL );
473
474         c->c_listener_url = ch_strdup( url  );
475         c->c_peer_domain = ch_strdup( dnsname  );
476     c->c_peer_name = ch_strdup( peername  );
477     c->c_sock_name = ch_strdup( sockname );
478
479     c->c_n_ops_received = 0;
480     c->c_n_ops_executing = 0;
481     c->c_n_ops_pending = 0;
482     c->c_n_ops_completed = 0;
483
484         c->c_n_get = 0;
485         c->c_n_read = 0;
486         c->c_n_write = 0;
487
488         /* set to zero until bind, implies LDAP_VERSION3 */
489         c->c_protocol = 0;
490
491 #ifdef SLAPD_MONITOR
492         c->c_activitytime = c->c_starttime = slap_get_time();
493 #else
494         if( global_idletimeout > 0 ) {
495                 c->c_activitytime = c->c_starttime = slap_get_time();
496         }
497 #endif
498
499 #ifdef LDAP_CONNECTIONLESS
500         c->c_is_udp = 0;
501         if (tls_udp_option == 2)
502         {
503                 c->c_is_udp = 1;
504 #ifdef LDAP_DEBUG
505         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
506                 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
507 #endif
508         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
509                 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
510         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
511                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
512         } else
513 #endif
514         {
515 #ifdef LDAP_DEBUG
516         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
517                 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
518 #endif
519         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
520                 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
521         }
522
523 #ifdef LDAP_DEBUG
524         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
525                 INT_MAX, (void*)"ldap_" );
526 #endif
527
528         if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
529                 c /* non-NULL */ ) < 0 )
530         {
531 #ifdef NEW_LOGGING
532                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
533                            "connection_init: conn %d  set nonblocking failed\n",
534                            c->c_connid ));
535 #else
536                 Debug( LDAP_DEBUG_ANY,
537                         "connection_init(%d, %s): set nonblocking failed\n",
538                         s, c->c_peer_name, 0 );
539 #endif
540         }
541
542     id = c->c_connid = conn_nextid++;
543
544     c->c_conn_state = SLAP_C_INACTIVE;
545     c->c_struct_state = SLAP_C_USED;
546
547         c->c_ssf = c->c_transport_ssf = ssf;
548         c->c_tls_ssf = 0;
549
550 #ifdef HAVE_TLS
551     if ( tls_udp_option == 1 ) {
552             c->c_is_tls = 1;
553             c->c_needs_tls_accept = 1;
554     } else {
555             c->c_is_tls = 0;
556             c->c_needs_tls_accept = 0;
557     }
558 #endif
559
560         slap_sasl_open( c );
561         slap_sasl_external( c, ssf, authid );
562
563     ldap_pvt_thread_mutex_unlock( &c->c_mutex );
564     ldap_pvt_thread_mutex_unlock( &connections_mutex );
565
566     backend_connection_init(c);
567
568     return id;
569 }
570
571 void connection2anonymous( Connection *c )
572 {
573     assert( connections != NULL );
574     assert( c != NULL );
575
576         {
577                 ber_len_t max = sockbuf_max_incoming;
578                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
579         }
580
581         if(c->c_authmech.bv_val != NULL ) {
582                 free(c->c_authmech.bv_val);
583                 c->c_authmech.bv_val = NULL;
584         }
585         c->c_authmech.bv_len = 0;
586
587     if(c->c_dn.bv_val != NULL) {
588         free(c->c_dn.bv_val);
589         c->c_dn.bv_val = NULL;
590     }
591     c->c_dn.bv_len = 0;
592     if(c->c_ndn.bv_val != NULL) {
593         free(c->c_ndn.bv_val);
594         c->c_ndn.bv_val = NULL;
595     }
596     c->c_ndn.bv_len = 0;
597
598         if(c->c_cdn.bv_val != NULL) {
599                 free(c->c_cdn.bv_val);
600                 c->c_cdn.bv_val = NULL;
601         }
602         c->c_cdn.bv_len = 0;
603
604         c->c_authc_backend = NULL;
605         c->c_authz_backend = NULL;
606     
607     {
608         GroupAssertion *g, *n;
609         for (g = c->c_groups; g; g=n)
610         {
611             n = g->next;
612             free(g);
613         }
614         c->c_groups = NULL;
615     }
616
617 }
618
619 static void
620 connection_destroy( Connection *c )
621 {
622         /* note: connections_mutex should be locked by caller */
623     ber_socket_t        sd;
624
625     assert( connections != NULL );
626     assert( c != NULL );
627     assert( c->c_struct_state != SLAP_C_UNUSED );
628     assert( c->c_conn_state != SLAP_C_INVALID );
629     assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
630
631     backend_connection_destroy(c);
632
633     c->c_protocol = 0;
634     c->c_connid = -1;
635
636     c->c_activitytime = c->c_starttime = 0;
637
638         connection2anonymous( c );
639
640         if(c->c_listener_url != NULL) {
641                 free(c->c_listener_url);
642                 c->c_listener_url = NULL;
643         }
644
645         if(c->c_peer_domain != NULL) {
646                 free(c->c_peer_domain);
647                 c->c_peer_domain = NULL;
648         }
649         if(c->c_peer_name != NULL) {
650 #ifdef LDAP_PF_lOCAL
651                 /*
652                  * If peer was a domain socket, unlink. Mind you,
653                  * they may be un-named. Should we leave this to
654                  * the client?
655                  */
656                 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
657                         char *path = c->c_peer_name + 5;
658                         if (path != '\0') {
659                                 (void)unlink(path);
660                         }
661                 }
662 #endif /* LDAP_PF_LOCAL */
663
664                 free(c->c_peer_name);
665                 c->c_peer_name = NULL;
666         }
667         if(c->c_sock_name != NULL) {
668                 free(c->c_sock_name);
669                 c->c_sock_name = NULL;
670         }
671
672         c->c_sasl_bind_in_progress = 0;
673         if(c->c_sasl_bind_mech.bv_val != NULL) {
674                 free(c->c_sasl_bind_mech.bv_val);
675                 c->c_sasl_bind_mech.bv_val = NULL;
676         }
677         c->c_sasl_bind_mech.bv_len = 0;
678
679         slap_sasl_close( c );
680
681         if ( c->c_currentber != NULL ) {
682                 ber_free( c->c_currentber, 1 );
683                 c->c_currentber = NULL;
684         }
685
686         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
687         if ( sd != AC_SOCKET_INVALID ) {
688                 slapd_remove( sd, 0 );
689
690                 Statslog( LDAP_DEBUG_STATS,
691                     "conn=%ld fd=%d closed\n",
692                         c->c_connid, sd, 0, 0, 0 );
693         }
694
695         ber_sockbuf_free( c->c_sb );
696
697         c->c_sb = ber_sockbuf_alloc( );
698
699         {
700                 ber_len_t max = sockbuf_max_incoming;
701                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
702         }
703
704     c->c_conn_state = SLAP_C_INVALID;
705     c->c_struct_state = SLAP_C_UNUSED;
706 }
707
708 int connection_state_closing( Connection *c )
709 {
710         /* c_mutex must be locked by caller */
711
712         int state;
713         assert( c != NULL );
714         assert( c->c_struct_state == SLAP_C_USED );
715
716         state = c->c_conn_state;
717
718         assert( state != SLAP_C_INVALID );
719
720         return state == SLAP_C_CLOSING;
721 }
722
723 static void connection_abandon( Connection *c )
724 {
725         /* c_mutex must be locked by caller */
726
727         Operation *o;
728
729         LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
730                 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
731                 o->o_abandon = 1;
732                 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
733         }
734
735         /* remove pending operations */
736         while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
737                 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
738                 LDAP_STAILQ_NEXT(o, o_next) = NULL;
739                 slap_op_free( o );
740         }
741 }
742
743 void connection_closing( Connection *c )
744 {
745         assert( connections != NULL );
746         assert( c != NULL );
747         assert( c->c_struct_state == SLAP_C_USED );
748         assert( c->c_conn_state != SLAP_C_INVALID );
749
750         /* c_mutex must be locked by caller */
751
752         if( c->c_conn_state != SLAP_C_CLOSING ) {
753                 ber_socket_t    sd;
754
755                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
756 #ifdef NEW_LOGGING
757                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
758                            "connection_closing: conn %d readying socket %d for close.\n",
759                            c->c_connid, sd ));
760 #else
761                 Debug( LDAP_DEBUG_TRACE,
762                         "connection_closing: readying conn=%ld sd=%d for close\n",
763                         c->c_connid, sd, 0 );
764 #endif
765                 /* update state to closing */
766                 c->c_conn_state = SLAP_C_CLOSING;
767
768                 /* don't listen on this port anymore */
769                 slapd_clr_read( sd, 1 );
770
771                 /* abandon active operations */
772                 connection_abandon( c );
773
774                 /* wake write blocked operations */
775                 slapd_clr_write( sd, 1 );
776                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
777         }
778 }
779
780 static void connection_close( Connection *c )
781 {
782         ber_socket_t    sd;
783
784         assert( connections != NULL );
785         assert( c != NULL );
786         assert( c->c_struct_state == SLAP_C_USED );
787         assert( c->c_conn_state == SLAP_C_CLOSING );
788
789         /* note: connections_mutex and c_mutex should be locked by caller */
790
791         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
792         if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
793 #ifdef NEW_LOGGING
794                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
795                            "connection_close: conn %d  deferring sd %d\n",
796                            c->c_connid, sd ));
797 #else
798                 Debug( LDAP_DEBUG_TRACE,
799                         "connection_close: deferring conn=%ld sd=%d\n",
800                         c->c_connid, sd, 0 );
801 #endif
802                 return;
803         }
804
805 #ifdef NEW_LOGGING
806         LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
807                    "connection_close: conn %d  sd %d\n",
808                    c->c_connid, sd ));
809 #else
810         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
811                 c->c_connid, sd, 0 );
812 #endif
813         connection_destroy( c );
814 }
815
816 unsigned long connections_nextid(void)
817 {
818         unsigned long id;
819         assert( connections != NULL );
820
821         ldap_pvt_thread_mutex_lock( &connections_mutex );
822
823         id = conn_nextid;
824
825         ldap_pvt_thread_mutex_unlock( &connections_mutex );
826
827         return id;
828 }
829
830 Connection* connection_first( ber_socket_t *index )
831 {
832         assert( connections != NULL );
833         assert( index != NULL );
834
835         ldap_pvt_thread_mutex_lock( &connections_mutex );
836
837         *index = 0;
838
839         return connection_next(NULL, index);
840 }
841
842 Connection* connection_next( Connection *c, ber_socket_t *index )
843 {
844         assert( connections != NULL );
845         assert( index != NULL );
846         assert( *index <= dtblsize );
847
848         if( c != NULL ) {
849                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
850         }
851
852         c = NULL;
853
854         for(; *index < dtblsize; (*index)++) {
855                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
856                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
857 #ifndef HAVE_WINSOCK
858                         continue;
859 #else
860                         break;
861 #endif
862                 }
863
864                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
865                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
866                         c = &connections[(*index)++];
867                         break;
868                 }
869
870                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
871                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
872         }
873
874         if( c != NULL ) {
875                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
876         }
877
878         return c;
879 }
880
881 void connection_done( Connection *c )
882 {
883         assert( connections != NULL );
884
885         if( c != NULL ) {
886                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
887         }
888
889         ldap_pvt_thread_mutex_unlock( &connections_mutex );
890 }
891
892 /*
893  * connection_activity - handle the request operation op on connection
894  * conn.  This routine figures out what kind of operation it is and
895  * calls the appropriate stub to handle it.
896  */
897
898 static void *
899 connection_operation( void *arg_v )
900 {
901         int rc;
902         struct co_arg   *arg = arg_v;
903         ber_tag_t tag = arg->co_op->o_tag;
904         Connection *conn = arg->co_conn;
905
906         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
907         num_ops_initiated++;
908         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
909
910         if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
911 #ifdef NEW_LOGGING
912                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
913                            "connection_operation: conn %d  SASL bind in progress (tag=%ld).\n",
914                            conn->c_connid, (long)tag ));
915 #else
916                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
917                         "error: SASL bind in progress (tag=%ld).\n",
918                         (long) tag, 0, 0 );
919 #endif
920                 send_ldap_result( conn, arg->co_op,
921                         rc = LDAP_OPERATIONS_ERROR,
922                         NULL, "SASL bind in progress", NULL, NULL );
923                 goto operations_error;
924         }
925
926         switch ( tag ) {
927         case LDAP_REQ_BIND:
928                 rc = do_bind( conn, arg->co_op );
929                 break;
930
931         case LDAP_REQ_UNBIND:
932                 rc = do_unbind( conn, arg->co_op );
933                 break;
934
935         case LDAP_REQ_ADD:
936                 rc = do_add( conn, arg->co_op );
937                 break;
938
939         case LDAP_REQ_DELETE:
940                 rc = do_delete( conn, arg->co_op );
941                 break;
942
943         case LDAP_REQ_MODRDN:
944                 rc = do_modrdn( conn, arg->co_op );
945                 break;
946
947         case LDAP_REQ_MODIFY:
948                 rc = do_modify( conn, arg->co_op );
949                 break;
950
951         case LDAP_REQ_COMPARE:
952                 rc = do_compare( conn, arg->co_op );
953                 break;
954
955         case LDAP_REQ_SEARCH:
956                 rc = do_search( conn, arg->co_op );
957                 break;
958
959         case LDAP_REQ_ABANDON:
960                 rc = do_abandon( conn, arg->co_op );
961                 break;
962
963         case LDAP_REQ_EXTENDED:
964                 rc = do_extended( conn, arg->co_op );
965                 break;
966
967         default:
968 #ifdef NEW_LOGGING
969                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
970                            "connection_operation: conn %d  unknown LDAP request 0x%lx\n",
971                            conn->c_connid, tag ));
972 #else
973                 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
974                     tag, 0, 0 );
975 #endif
976                 arg->co_op->o_tag = LBER_ERROR;
977                 send_ldap_disconnect( conn, arg->co_op,
978                         LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
979                 rc = -1;
980                 break;
981         }
982
983         if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
984
985 operations_error:
986         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
987         num_ops_completed++;
988         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
989
990         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
991
992         conn->c_n_ops_executing--;
993         conn->c_n_ops_completed++;
994
995         LDAP_STAILQ_REMOVE( &conn->c_ops, arg->co_op, slap_op, o_next);
996         LDAP_STAILQ_NEXT(arg->co_op, o_next) = NULL;
997         slap_op_free( arg->co_op );
998         arg->co_op = NULL;
999         arg->co_conn = NULL;
1000         free( (char *) arg );
1001         arg = NULL;
1002
1003         switch( tag ) {
1004         case LBER_ERROR:
1005         case LDAP_REQ_UNBIND:
1006                 /* c_mutex is locked */
1007                 connection_closing( conn );
1008                 break;
1009
1010         case LDAP_REQ_BIND:
1011                 conn->c_sasl_bind_in_progress =
1012                         rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1013
1014                 if( conn->c_conn_state == SLAP_C_BINDING) {
1015                         conn->c_conn_state = SLAP_C_ACTIVE;
1016                 }
1017         }
1018
1019         connection_resched( conn );
1020
1021         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1022
1023         return NULL;
1024 }
1025
1026 int connection_read(ber_socket_t s)
1027 {
1028         int rc = 0;
1029         Connection *c;
1030         assert( connections != NULL );
1031
1032         ldap_pvt_thread_mutex_lock( &connections_mutex );
1033
1034         /* get (locked) connection */
1035         c = connection_get( s );
1036
1037         if( c == NULL ) {
1038 #ifdef NEW_LOGGING
1039                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1040                            "connection_read: sock %ld no connection\n",
1041                            (long)s ));
1042 #else
1043                 Debug( LDAP_DEBUG_ANY,
1044                         "connection_read(%ld): no connection!\n",
1045                         (long) s, 0, 0 );
1046 #endif
1047                 slapd_remove(s, 0);
1048
1049                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1050                 return -1;
1051         }
1052
1053         c->c_n_read++;
1054
1055         if( c->c_conn_state == SLAP_C_CLOSING ) {
1056 #ifdef NEW_LOGGING
1057                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1058                            "connection_read: conn %d connection closing, ignoring input\n",
1059                            c->c_connid));
1060 #else
1061                 Debug( LDAP_DEBUG_TRACE,
1062                         "connection_read(%d): closing, ignoring input for id=%ld\n",
1063                         s, c->c_connid, 0 );
1064 #endif
1065                 connection_return( c );
1066                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1067                 return 0;
1068         }
1069
1070 #ifdef NEW_LOGGING
1071         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1072                    "connection_read: conn %d  checking for input.\n", c->c_connid ));
1073 #else
1074         Debug( LDAP_DEBUG_TRACE,
1075                 "connection_read(%d): checking for input on id=%ld\n",
1076                 s, c->c_connid, 0 );
1077 #endif
1078
1079 #ifdef HAVE_TLS
1080         if ( c->c_is_tls && c->c_needs_tls_accept ) {
1081                 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1082                 if ( rc < 0 ) {
1083 #if 0 /* required by next #if 0 */
1084                         struct timeval tv;
1085                         fd_set rfd;
1086 #endif
1087
1088 #ifdef NEW_LOGGING
1089                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1090                                    "connection_read: conn %d  TLS accept error, error %d\n",
1091                                    c->c_connid, rc ));
1092 #else
1093                         Debug( LDAP_DEBUG_TRACE,
1094                                 "connection_read(%d): TLS accept error "
1095                                 "error=%d id=%ld, closing\n",
1096                                 s, rc, c->c_connid );
1097 #endif
1098                         c->c_needs_tls_accept = 0;
1099                         /* connections_mutex and c_mutex are locked */
1100                         connection_closing( c );
1101
1102 #if 0
1103                         /* Drain input before close, to allow SSL error codes
1104                          * to propagate to client. */
1105                         FD_ZERO(&rfd);
1106                         FD_SET(s, &rfd);
1107                         for (rc=1; rc>0;)
1108                         {
1109                             tv.tv_sec = 1;
1110                             tv.tv_usec = 0;
1111                             rc = select(s+1, &rfd, NULL, NULL, &tv);
1112                             if (rc == 1)
1113                                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
1114                                     NULL);
1115                         }
1116 #endif
1117                         connection_close( c );
1118
1119                 } else if ( rc == 0 ) {
1120                         void *ssl;
1121                         char *authid;
1122
1123                         c->c_needs_tls_accept = 0;
1124
1125                         /* we need to let SASL know */
1126                         ssl = (void *)ldap_pvt_tls_sb_ctx( c->c_sb );
1127
1128                         c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1129                         if( c->c_tls_ssf > c->c_ssf ) {
1130                                 c->c_ssf = c->c_tls_ssf;
1131                         }
1132
1133                         authid = (char *)ldap_pvt_tls_get_peer( ssl );
1134                         slap_sasl_external( c, c->c_tls_ssf, authid );
1135                 }
1136                 connection_return( c );
1137                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1138                 return 0;
1139         }
1140 #endif
1141
1142 #ifdef HAVE_CYRUS_SASL
1143         if ( c->c_sasl_layers ) {
1144                 c->c_sasl_layers = 0;
1145
1146                 rc = ldap_pvt_sasl_install( c->c_sb,  c->c_sasl_context );
1147
1148                 if( rc != LDAP_SUCCESS ) {
1149 #ifdef NEW_LOGGING
1150                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1151                                    "connection_read: conn %d SASL install error %d, closing\n",
1152                                    c->c_connid, rc ));
1153 #else
1154                         Debug( LDAP_DEBUG_TRACE,
1155                                 "connection_read(%d): SASL install error "
1156                                 "error=%d id=%ld, closing\n",
1157                                 s, rc, c->c_connid );
1158 #endif
1159                         /* connections_mutex and c_mutex are locked */
1160                         connection_closing( c );
1161                         connection_close( c );
1162                         connection_return( c );
1163                         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1164                         return 0;
1165                 }
1166         }
1167 #endif
1168
1169 #define CONNECTION_INPUT_LOOP 1
1170
1171 #ifdef DATA_READY_LOOP
1172         while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1173 #elif CONNECTION_INPUT_LOOP
1174         while(!rc)
1175 #endif
1176         {
1177                 /* How do we do this without getting into a busy loop ? */
1178                 rc = connection_input( c );
1179         }
1180
1181         if( rc < 0 ) {
1182 #ifdef NEW_LOGGING
1183                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1184                            "connection_read: conn %d  input error %d, closing.\n",
1185                            c->c_connid, rc ));
1186 #else
1187                 Debug( LDAP_DEBUG_TRACE,
1188                         "connection_read(%d): input error=%d id=%ld, closing.\n",
1189                         s, rc, c->c_connid );
1190 #endif
1191                 /* connections_mutex and c_mutex are locked */
1192                 connection_closing( c );
1193                 connection_close( c );
1194                 connection_return( c );
1195                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1196                 return 0;
1197         }
1198
1199         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1200                 slapd_set_read( s, 1 );
1201         }
1202
1203         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1204                 slapd_set_write( s, 1 );
1205         }
1206
1207         connection_return( c );
1208         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1209         return 0;
1210 }
1211
1212 static int
1213 connection_input(
1214     Connection *conn
1215 )
1216 {
1217         Operation *op;
1218         ber_tag_t       tag;
1219         ber_len_t       len;
1220         ber_int_t       msgid;
1221         BerElement      *ber;
1222 #ifdef LDAP_CONNECTIONLESS
1223         Sockaddr        peeraddr;
1224         char            *cdn = NULL;
1225 #endif
1226
1227         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1228             == NULL ) {
1229 #ifdef NEW_LOGGING
1230                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1231                            "connection_input: conn %d  ber_alloc failed.\n", conn->c_connid ));
1232 #else
1233                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1234 #endif
1235                 return -1;
1236         }
1237
1238         errno = 0;
1239
1240 #ifdef LDAP_CONNECTIONLESS
1241         if (conn->c_is_udp)
1242         {
1243                 char    peername[sizeof("IP=255.255.255.255:65336")];
1244                 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1245                         sizeof(struct sockaddr));
1246                 if (len != sizeof(struct sockaddr))
1247                         return 1;
1248                 sprintf( peername, "IP=%s:%d",
1249                         inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1250                         (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1251                 Statslog( LDAP_DEBUG_STATS,
1252                             "conn=%ld UDP request from %s (%s) accepted.\n",
1253                             conn->c_connid, peername,
1254                             conn->c_sock_name, 0, 0 );
1255         }
1256 #endif
1257         tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1258         if ( tag != LDAP_TAG_MESSAGE ) {
1259                 int err = errno;
1260                 ber_socket_t    sd;
1261
1262                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1263
1264 #ifdef NEW_LOGGING
1265                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1266                            "connection_input: conn %d  ber_get_next failed, errno %d (%s).\n",
1267                            conn->c_connid, err, sock_errstr(err) ));
1268 #else
1269                 Debug( LDAP_DEBUG_TRACE,
1270                         "ber_get_next on fd %d failed errno=%d (%s)\n",
1271                         sd, err, sock_errstr(err) );
1272 #endif
1273                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1274                         /* log, close and send error */
1275                         ber_free( conn->c_currentber, 1 );
1276                         conn->c_currentber = NULL;
1277
1278                         return -2;
1279                 }
1280                 return 1;
1281         }
1282
1283         ber = conn->c_currentber;
1284         conn->c_currentber = NULL;
1285
1286         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1287                 /* log, close and send error */
1288 #ifdef NEW_LOGGING
1289                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1290                            "connection_input: conn %d  ber_get_int returns 0x%lx.\n",
1291                            conn->c_connid, tag ));
1292 #else
1293                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1294                     0 );
1295 #endif
1296                 ber_free( ber, 1 );
1297                 return -1;
1298         }
1299
1300         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1301                 /* log, close and send error */
1302 #ifdef NEW_LOGGING
1303                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1304                            "connection_input: conn %d  ber_peek_tag returns 0x%lx.\n",
1305                            conn->c_connid, tag ));
1306 #else
1307                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1308                     0 );
1309 #endif
1310                 ber_free( ber, 1 );
1311
1312                 return -1;
1313         }
1314
1315 #ifdef LDAP_CONNECTIONLESS
1316         if (conn->c_is_udp) {
1317                 if (tag == LBER_OCTETSTRING) {
1318                         ber_get_stringa( ber, &cdn );
1319                         tag = ber_peek_tag(ber, &len);
1320                 }
1321                 if (tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH) {
1322 #ifdef NEW_LOGGING
1323                     LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1324                                "connection_input: conn %d  invalid req for UDP 0x%lx.\n",
1325                                conn->c_connid, tag ));
1326 #else
1327                     Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1328                         0 );
1329 #endif
1330                     ber_free( ber, 1 );
1331                     return 0;
1332                 }
1333         }
1334 #endif
1335         if(tag == LDAP_REQ_BIND) {
1336                 /* immediately abandon all exiting operations upon BIND */
1337                 connection_abandon( conn );
1338         }
1339
1340         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1341
1342 #ifdef LDAP_CONNECTIONLESS
1343         op->o_peeraddr = peeraddr;
1344         if (cdn) {
1345             op->o_dn = cdn;
1346             op->o_protocol = LDAP_VERSION2;
1347         }
1348 #endif
1349         if ( conn->c_conn_state == SLAP_C_BINDING
1350                 || conn->c_conn_state == SLAP_C_CLOSING )
1351         {
1352 #ifdef NEW_LOGGING
1353                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1354                            "connection_input: conn %d  deferring operation\n",
1355                            conn->c_connid ));
1356 #else
1357                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1358 #endif
1359                 conn->c_n_ops_pending++;
1360                 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1361
1362         } else {
1363                 conn->c_n_ops_executing++;
1364                 connection_op_activate( conn, op );
1365         }
1366
1367 #ifdef NO_THREADS
1368         if ( conn->c_struct_state != SLAP_C_USED ) {
1369                 /* connection must have got closed underneath us */
1370                 return 1;
1371         }
1372 #endif
1373         assert( conn->c_struct_state == SLAP_C_USED );
1374
1375         return 0;
1376 }
1377
1378 static int
1379 connection_resched( Connection *conn )
1380 {
1381         Operation *op;
1382
1383         if( conn->c_conn_state == SLAP_C_CLOSING ) {
1384                 int rc;
1385                 ber_socket_t    sd;
1386                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1387
1388                 /* us trylock to avoid possible deadlock */
1389                 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1390
1391                 if( rc ) {
1392 #ifdef NEW_LOGGING
1393                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1394                                    "connection_resched: conn %d  reaquiring locks.\n",
1395                                    conn->c_connid ));
1396 #else
1397                         Debug( LDAP_DEBUG_TRACE,
1398                                 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1399                                 conn->c_connid, sd, 0 );
1400 #endif
1401                         /*
1402                          * reaquire locks in the right order...
1403                          * this may allow another thread to close this connection,
1404                          * so recheck state below.
1405                          */
1406                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1407                         ldap_pvt_thread_mutex_lock( &connections_mutex );
1408                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1409                 }
1410
1411                 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1412 #ifdef NEW_LOGGING
1413                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1414                                    "connection_resched: conn %d  closed by other thread.\n",
1415                                    conn->c_connid ));
1416 #else
1417                         Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1418                                 "closed by other thread conn=%ld sd=%d\n",
1419                                 conn->c_connid, sd, 0 );
1420 #endif
1421                 } else {
1422 #ifdef NEW_LOGGING
1423                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1424                                    "connection_resched: conn %d  attempting closing.\n",
1425                                    conn->c_connid ));
1426 #else
1427                         Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1428                                 "attempting closing conn=%ld sd=%d\n",
1429                                 conn->c_connid, sd, 0 );
1430 #endif
1431                         connection_close( conn );
1432                 }
1433
1434                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1435                 return 0;
1436         }
1437
1438         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1439                 /* other states need different handling */
1440                 return 0;
1441         }
1442
1443         while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1444                 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1445                 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1446                 /* pending operations should not be marked for abandonment */
1447                 assert(!op->o_abandon);
1448
1449                 conn->c_n_ops_pending--;
1450                 conn->c_n_ops_executing++;
1451
1452                 connection_op_activate( conn, op );
1453
1454                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1455                         break;
1456                 }
1457         }
1458         return 0;
1459 }
1460
1461 static int connection_op_activate( Connection *conn, Operation *op )
1462 {
1463         struct co_arg *arg;
1464         int status;
1465         ber_tag_t tag = op->o_tag;
1466
1467         if(tag == LDAP_REQ_BIND) {
1468                 conn->c_conn_state = SLAP_C_BINDING;
1469         }
1470
1471         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1472         arg->co_conn = conn;
1473         arg->co_op = op;
1474
1475         if (!arg->co_op->o_dn.bv_len) {
1476             arg->co_op->o_authz = conn->c_authz;
1477             arg->co_op->o_dn.bv_val = ch_strdup( conn->c_dn.bv_val ?
1478                 conn->c_dn.bv_val : "" );
1479             arg->co_op->o_ndn.bv_val = ch_strdup( conn->c_ndn.bv_val ?
1480                 conn->c_ndn.bv_val : "" );
1481         }
1482         arg->co_op->o_authtype = conn->c_authtype;
1483         ber_dupbv( &arg->co_op->o_authmech, &conn->c_authmech );
1484         
1485         if (!arg->co_op->o_protocol) {
1486             arg->co_op->o_protocol = conn->c_protocol
1487                 ? conn->c_protocol : LDAP_VERSION3;
1488         }
1489         arg->co_op->o_connid = conn->c_connid;
1490
1491         LDAP_STAILQ_INSERT_TAIL( &conn->c_ops, arg->co_op, o_next );
1492
1493         status = ldap_pvt_thread_pool_submit( &connection_pool,
1494                 connection_operation, (void *) arg );
1495
1496         if ( status != 0 ) {
1497 #ifdef NEW_LOGGING
1498                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1499                            "connection_op_activate: conn %d      thread pool submit failed.\n",
1500                            conn->c_connid ));
1501 #else
1502                 Debug( LDAP_DEBUG_ANY,
1503                 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1504 #endif
1505                 /* should move op to pending list */
1506         }
1507
1508         return status;
1509 }
1510
1511 int connection_write(ber_socket_t s)
1512 {
1513         Connection *c;
1514         assert( connections != NULL );
1515
1516         ldap_pvt_thread_mutex_lock( &connections_mutex );
1517
1518         c = connection_get( s );
1519
1520         slapd_clr_write( s, 0);
1521
1522         if( c == NULL ) {
1523 #ifdef NEW_LOGGING
1524                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1525                            "connection_write: sock %ld  no connection!\n",(long)s));
1526 #else
1527                 Debug( LDAP_DEBUG_ANY,
1528                         "connection_write(%ld): no connection!\n",
1529                         (long) s, 0, 0 );
1530 #endif
1531                 slapd_remove(s, 0);
1532                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1533                 return -1;
1534         }
1535
1536         c->c_n_write++;
1537
1538 #ifdef NEW_LOGGING
1539         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1540                    "connection_write conn %d  waking output.\n",
1541                    c->c_connid ));
1542 #else
1543         Debug( LDAP_DEBUG_TRACE,
1544                 "connection_write(%d): waking output for id=%ld\n",
1545                 s, c->c_connid, 0 );
1546 #endif
1547         ldap_pvt_thread_cond_signal( &c->c_write_cv );
1548
1549         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1550                 slapd_set_read( s, 1 );
1551         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1552                 slapd_set_write( s, 1 );
1553         connection_return( c );
1554         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1555         return 0;
1556 }
1557