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