]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
more cleanup from jon@symas.com
[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 /* 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 = NULL;
416                 c->c_dn = NULL;
417                 c->c_ndn = NULL;
418                 c->c_cdn = NULL;
419                 c->c_groups = NULL;
420
421                 c->c_listener_url = NULL;
422                 c->c_peer_domain = NULL;
423                 c->c_peer_name = NULL;
424                 c->c_sock_name = NULL;
425
426                 c->c_ops = NULL;
427                 c->c_pending_ops = NULL;
428
429                 c->c_sasl_bind_mech = NULL;
430                 c->c_sasl_context = NULL;
431                 c->c_sasl_extra = NULL;
432
433                 c->c_sb = ber_sockbuf_alloc( );
434
435                 {
436                         ber_len_t max = sockbuf_max_incoming;
437                         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
438                 }
439
440                 c->c_currentber = NULL;
441
442                 /* should check status of thread calls */
443                 ldap_pvt_thread_mutex_init( &c->c_mutex );
444                 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
445                 ldap_pvt_thread_cond_init( &c->c_write_cv );
446
447                 c->c_struct_state = SLAP_C_UNUSED;
448         }
449
450     ldap_pvt_thread_mutex_lock( &c->c_mutex );
451
452     assert( c->c_struct_state == SLAP_C_UNUSED );
453         assert( c->c_authmech == NULL );
454     assert(     c->c_dn == NULL );
455     assert(     c->c_ndn == NULL );
456     assert(     c->c_cdn == NULL );
457     assert( c->c_groups == NULL );
458     assert( c->c_listener_url == NULL );
459     assert( c->c_peer_domain == NULL );
460     assert( c->c_peer_name == NULL );
461     assert( c->c_sock_name == NULL );
462     assert( c->c_ops == NULL );
463     assert( c->c_pending_ops == NULL );
464         assert( c->c_sasl_bind_mech == NULL );
465         assert( c->c_sasl_context == NULL );
466         assert( c->c_sasl_extra == NULL );
467         assert( c->c_currentber == NULL );
468
469         c->c_listener_url = ch_strdup( url  );
470         c->c_peer_domain = ch_strdup( dnsname  );
471     c->c_peer_name = ch_strdup( peername  );
472     c->c_sock_name = ch_strdup( sockname );
473
474     c->c_n_ops_received = 0;
475     c->c_n_ops_executing = 0;
476     c->c_n_ops_pending = 0;
477     c->c_n_ops_completed = 0;
478
479         c->c_n_get = 0;
480         c->c_n_read = 0;
481         c->c_n_write = 0;
482
483         /* set to zero until bind, implies LDAP_VERSION3 */
484         c->c_protocol = 0;
485
486 #ifdef SLAPD_MONITOR
487         c->c_activitytime = c->c_starttime = slap_get_time();
488 #else
489         if( global_idletimeout > 0 ) {
490                 c->c_activitytime = c->c_starttime = slap_get_time();
491         }
492 #endif
493
494 #ifdef LDAP_CONNECTIONLESS
495         c->c_is_udp = 0;
496         if (tls_udp_option == 2)
497         {
498                 c->c_is_udp = 1;
499 #ifdef LDAP_DEBUG
500         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
501                 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
502 #endif
503         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
504                 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
505         } else
506 #endif
507         {
508 #ifdef LDAP_DEBUG
509         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
510                 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
511 #endif
512         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
513                 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
514         }
515         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
516                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
517
518 #ifdef LDAP_DEBUG
519         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
520                 INT_MAX, (void*)"ldap_" );
521 #endif
522
523         if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
524                 c /* non-NULL */ ) < 0 )
525         {
526 #ifdef NEW_LOGGING
527                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
528                            "connection_init: conn %d  set nonblocking failed\n",
529                            c->c_connid ));
530 #else
531                 Debug( LDAP_DEBUG_ANY,
532                         "connection_init(%d, %s): set nonblocking failed\n",
533                         s, c->c_peer_name, 0 );
534 #endif
535         }
536
537     id = c->c_connid = conn_nextid++;
538
539     c->c_conn_state = SLAP_C_INACTIVE;
540     c->c_struct_state = SLAP_C_USED;
541
542         c->c_ssf = c->c_transport_ssf = ssf;
543         c->c_tls_ssf = 0;
544
545 #ifdef HAVE_TLS
546     if ( tls_udp_option == 1 ) {
547             c->c_is_tls = 1;
548             c->c_needs_tls_accept = 1;
549     } else {
550             c->c_is_tls = 0;
551             c->c_needs_tls_accept = 0;
552     }
553 #endif
554
555         slap_sasl_open( c );
556         slap_sasl_external( c, ssf, authid );
557
558     ldap_pvt_thread_mutex_unlock( &c->c_mutex );
559     ldap_pvt_thread_mutex_unlock( &connections_mutex );
560
561     backend_connection_init(c);
562
563     return id;
564 }
565
566 void connection2anonymous( Connection *c )
567 {
568     assert( connections != NULL );
569     assert( c != NULL );
570
571         {
572                 ber_len_t max = sockbuf_max_incoming;
573                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
574         }
575
576         if(c->c_authmech != NULL ) {
577                 free(c->c_authmech);
578                 c->c_authmech = NULL;
579         }
580
581     if(c->c_dn != NULL) {
582         free(c->c_dn);
583         c->c_dn = NULL;
584     }
585     if(c->c_ndn != NULL) {
586         free(c->c_ndn);
587         c->c_ndn = NULL;
588     }
589
590         if(c->c_cdn != NULL) {
591                 free(c->c_cdn);
592                 c->c_cdn = NULL;
593         }
594
595         c->c_authc_backend = NULL;
596         c->c_authz_backend = NULL;
597     
598     {
599         GroupAssertion *g, *n;
600         for (g = c->c_groups; g; g=n)
601         {
602             n = g->next;
603             free(g);
604         }
605         c->c_groups = NULL;
606     }
607
608 }
609
610 static void
611 connection_destroy( Connection *c )
612 {
613         /* note: connections_mutex should be locked by caller */
614     ber_socket_t        sd;
615
616     assert( connections != NULL );
617     assert( c != NULL );
618     assert( c->c_struct_state != SLAP_C_UNUSED );
619     assert( c->c_conn_state != SLAP_C_INVALID );
620     assert( c->c_ops == NULL );
621
622     backend_connection_destroy(c);
623
624     c->c_protocol = 0;
625     c->c_connid = -1;
626
627     c->c_activitytime = c->c_starttime = 0;
628
629         connection2anonymous( c );
630
631         if(c->c_listener_url != NULL) {
632                 free(c->c_listener_url);
633                 c->c_listener_url = NULL;
634         }
635
636         if(c->c_peer_domain != NULL) {
637                 free(c->c_peer_domain);
638                 c->c_peer_domain = NULL;
639         }
640         if(c->c_peer_name != NULL) {
641 #ifdef LDAP_PF_lOCAL
642                 /*
643                  * If peer was a domain socket, unlink. Mind you,
644                  * they may be un-named. Should we leave this to
645                  * the client?
646                  */
647                 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
648                         char *path = c->c_peer_name + 5;
649                         if (path != '\0') {
650                                 (void)unlink(path);
651                         }
652                 }
653 #endif /* LDAP_PF_LOCAL */
654
655                 free(c->c_peer_name);
656                 c->c_peer_name = NULL;
657         }
658         if(c->c_sock_name != NULL) {
659                 free(c->c_sock_name);
660                 c->c_sock_name = NULL;
661         }
662
663         c->c_sasl_bind_in_progress = 0;
664         if(c->c_sasl_bind_mech != NULL) {
665                 free(c->c_sasl_bind_mech);
666                 c->c_sasl_bind_mech = NULL;
667         }
668
669         slap_sasl_close( c );
670
671         if ( c->c_currentber != NULL ) {
672                 ber_free( c->c_currentber, 1 );
673                 c->c_currentber = NULL;
674         }
675
676         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
677         if ( sd != AC_SOCKET_INVALID ) {
678                 slapd_remove( sd, 0 );
679
680                 Statslog( LDAP_DEBUG_STATS,
681                     "conn=%ld fd=%d closed\n",
682                         c->c_connid, sd, 0, 0, 0 );
683         }
684
685         ber_sockbuf_free( c->c_sb );
686
687         c->c_sb = ber_sockbuf_alloc( );
688
689         {
690                 ber_len_t max = sockbuf_max_incoming;
691                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
692         }
693
694     c->c_conn_state = SLAP_C_INVALID;
695     c->c_struct_state = SLAP_C_UNUSED;
696 }
697
698 int connection_state_closing( Connection *c )
699 {
700         /* c_mutex must be locked by caller */
701
702         int state;
703         assert( c != NULL );
704         assert( c->c_struct_state == SLAP_C_USED );
705
706         state = c->c_conn_state;
707
708         assert( state != SLAP_C_INVALID );
709
710         return state == SLAP_C_CLOSING;
711 }
712
713 static void connection_abandon( Connection *c )
714 {
715         /* c_mutex must be locked by caller */
716
717         Operation *o;
718
719         for( o = c->c_ops; o != NULL; o = o->o_next ) {
720                 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
721                 o->o_abandon = 1;
722                 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
723         }
724
725         /* remove pending operations */
726         for( o = slap_op_pop( &c->c_pending_ops );
727                 o != NULL;
728                 o = slap_op_pop( &c->c_pending_ops ) )
729         {
730                 slap_op_free( o );
731         }
732 }
733
734 void connection_closing( Connection *c )
735 {
736         assert( connections != NULL );
737         assert( c != NULL );
738         assert( c->c_struct_state == SLAP_C_USED );
739         assert( c->c_conn_state != SLAP_C_INVALID );
740
741         /* c_mutex must be locked by caller */
742
743         if( c->c_conn_state != SLAP_C_CLOSING ) {
744                 ber_socket_t    sd;
745
746                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
747 #ifdef NEW_LOGGING
748                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
749                            "connection_closing: conn %d readying socket %d for close.\n",
750                            c->c_connid, sd ));
751 #else
752                 Debug( LDAP_DEBUG_TRACE,
753                         "connection_closing: readying conn=%ld sd=%d for close\n",
754                         c->c_connid, sd, 0 );
755 #endif
756                 /* update state to closing */
757                 c->c_conn_state = SLAP_C_CLOSING;
758
759                 /* don't listen on this port anymore */
760                 slapd_clr_read( sd, 1 );
761
762                 /* abandon active operations */
763                 connection_abandon( c );
764
765                 /* wake write blocked operations */
766                 slapd_clr_write( sd, 1 );
767                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
768         }
769 }
770
771 static void connection_close( Connection *c )
772 {
773         ber_socket_t    sd;
774
775         assert( connections != NULL );
776         assert( c != NULL );
777         assert( c->c_struct_state == SLAP_C_USED );
778         assert( c->c_conn_state == SLAP_C_CLOSING );
779
780         /* note: connections_mutex and c_mutex should be locked by caller */
781
782         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
783         if( c->c_ops != NULL ) {
784 #ifdef NEW_LOGGING
785                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
786                            "connection_close: conn %d  deferring sd %d\n",
787                            c->c_connid, sd ));
788 #else
789                 Debug( LDAP_DEBUG_TRACE,
790                         "connection_close: deferring conn=%ld sd=%d\n",
791                         c->c_connid, sd, 0 );
792 #endif
793                 return;
794         }
795
796 #ifdef NEW_LOGGING
797         LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
798                    "connection_close: conn %d  sd %d\n",
799                    c->c_connid, sd ));
800 #else
801         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
802                 c->c_connid, sd, 0 );
803 #endif
804         connection_destroy( c );
805 }
806
807 unsigned long connections_nextid(void)
808 {
809         unsigned long id;
810         assert( connections != NULL );
811
812         ldap_pvt_thread_mutex_lock( &connections_mutex );
813
814         id = conn_nextid;
815
816         ldap_pvt_thread_mutex_unlock( &connections_mutex );
817
818         return id;
819 }
820
821 Connection* connection_first( ber_socket_t *index )
822 {
823         assert( connections != NULL );
824         assert( index != NULL );
825
826         ldap_pvt_thread_mutex_lock( &connections_mutex );
827
828         *index = 0;
829
830         return connection_next(NULL, index);
831 }
832
833 Connection* connection_next( Connection *c, ber_socket_t *index )
834 {
835         assert( connections != NULL );
836         assert( index != NULL );
837         assert( *index <= dtblsize );
838
839         if( c != NULL ) {
840                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
841         }
842
843         c = NULL;
844
845         for(; *index < dtblsize; (*index)++) {
846                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
847                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
848 #ifndef HAVE_WINSOCK
849                         continue;
850 #else
851                         break;
852 #endif
853                 }
854
855                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
856                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
857                         c = &connections[(*index)++];
858                         break;
859                 }
860
861                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
862                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
863         }
864
865         if( c != NULL ) {
866                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
867         }
868
869         return c;
870 }
871
872 void connection_done( Connection *c )
873 {
874         assert( connections != NULL );
875
876         if( c != NULL ) {
877                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
878         }
879
880         ldap_pvt_thread_mutex_unlock( &connections_mutex );
881 }
882
883 /*
884  * connection_activity - handle the request operation op on connection
885  * conn.  This routine figures out what kind of operation it is and
886  * calls the appropriate stub to handle it.
887  */
888
889 static void *
890 connection_operation( void *arg_v )
891 {
892         int rc;
893         struct co_arg   *arg = arg_v;
894         ber_tag_t tag = arg->co_op->o_tag;
895         Connection *conn = arg->co_conn;
896
897         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
898         num_ops_initiated++;
899         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
900
901         if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
902 #ifdef NEW_LOGGING
903                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
904                            "connection_operation: conn %d  SASL bind in progress (tag=%ld).\n",
905                            conn->c_connid, (long)tag ));
906 #else
907                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
908                         "error: SASL bind in progress (tag=%ld).\n",
909                         (long) tag, 0, 0 );
910 #endif
911                 send_ldap_result( conn, arg->co_op,
912                         rc = LDAP_OPERATIONS_ERROR,
913                         NULL, "SASL bind in progress", NULL, NULL );
914                 goto operations_error;
915         }
916
917         switch ( tag ) {
918         case LDAP_REQ_BIND:
919                 rc = do_bind( conn, arg->co_op );
920                 break;
921
922         case LDAP_REQ_UNBIND:
923                 rc = do_unbind( conn, arg->co_op );
924                 break;
925
926         case LDAP_REQ_ADD:
927                 rc = do_add( conn, arg->co_op );
928                 break;
929
930         case LDAP_REQ_DELETE:
931                 rc = do_delete( conn, arg->co_op );
932                 break;
933
934         case LDAP_REQ_MODRDN:
935                 rc = do_modrdn( conn, arg->co_op );
936                 break;
937
938         case LDAP_REQ_MODIFY:
939                 rc = do_modify( conn, arg->co_op );
940                 break;
941
942         case LDAP_REQ_COMPARE:
943                 rc = do_compare( conn, arg->co_op );
944                 break;
945
946         case LDAP_REQ_SEARCH:
947                 rc = do_search( conn, arg->co_op );
948                 break;
949
950         case LDAP_REQ_ABANDON:
951                 rc = do_abandon( conn, arg->co_op );
952                 break;
953
954         case LDAP_REQ_EXTENDED:
955                 rc = do_extended( conn, arg->co_op );
956                 break;
957
958         default:
959 #ifdef NEW_LOGGING
960                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
961                            "connection_operation: conn %d  unknown LDAP request 0x%lx\n",
962                            conn->c_connid, tag ));
963 #else
964                 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
965                     tag, 0, 0 );
966 #endif
967                 arg->co_op->o_tag = LBER_ERROR;
968                 send_ldap_disconnect( conn, arg->co_op,
969                         LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
970                 rc = -1;
971                 break;
972         }
973
974         if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
975
976 operations_error:
977         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
978         num_ops_completed++;
979         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
980
981         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
982
983         conn->c_n_ops_executing--;
984         conn->c_n_ops_completed++;
985
986         slap_op_remove( &conn->c_ops, arg->co_op );
987         slap_op_free( arg->co_op );
988         arg->co_op = NULL;
989         arg->co_conn = NULL;
990         free( (char *) arg );
991         arg = NULL;
992
993         switch( tag ) {
994         case LBER_ERROR:
995         case LDAP_REQ_UNBIND:
996                 /* c_mutex is locked */
997                 connection_closing( conn );
998                 break;
999
1000         case LDAP_REQ_BIND:
1001                 conn->c_sasl_bind_in_progress =
1002                         rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1003
1004                 if( conn->c_conn_state == SLAP_C_BINDING) {
1005                         conn->c_conn_state = SLAP_C_ACTIVE;
1006                 }
1007         }
1008
1009         connection_resched( conn );
1010
1011         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1012
1013         return NULL;
1014 }
1015
1016 int connection_read(ber_socket_t s)
1017 {
1018         int rc = 0;
1019         Connection *c;
1020         assert( connections != NULL );
1021
1022         ldap_pvt_thread_mutex_lock( &connections_mutex );
1023
1024         /* get (locked) connection */
1025         c = connection_get( s );
1026
1027         if( c == NULL ) {
1028 #ifdef NEW_LOGGING
1029                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1030                            "connection_read: sock %ld no connection\n",
1031                            (long)s ));
1032 #else
1033                 Debug( LDAP_DEBUG_ANY,
1034                         "connection_read(%ld): no connection!\n",
1035                         (long) s, 0, 0 );
1036 #endif
1037                 slapd_remove(s, 0);
1038
1039                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1040                 return -1;
1041         }
1042
1043         c->c_n_read++;
1044
1045         if( c->c_conn_state == SLAP_C_CLOSING ) {
1046 #ifdef NEW_LOGGING
1047                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1048                            "connection_read: conn %d connection closing, ignoring input\n",
1049                            c->c_connid));
1050 #else
1051                 Debug( LDAP_DEBUG_TRACE,
1052                         "connection_read(%d): closing, ignoring input for id=%ld\n",
1053                         s, c->c_connid, 0 );
1054 #endif
1055                 connection_return( c );
1056                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1057                 return 0;
1058         }
1059
1060 #ifdef NEW_LOGGING
1061         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1062                    "connection_read: conn %d  checking for input.\n", c->c_connid ));
1063 #else
1064         Debug( LDAP_DEBUG_TRACE,
1065                 "connection_read(%d): checking for input on id=%ld\n",
1066                 s, c->c_connid, 0 );
1067 #endif
1068
1069 #ifdef HAVE_TLS
1070         if ( c->c_is_tls && c->c_needs_tls_accept ) {
1071                 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1072                 if ( rc < 0 ) {
1073 #if 0 /* required by next #if 0 */
1074                         struct timeval tv;
1075                         fd_set rfd;
1076 #endif
1077
1078 #ifdef NEW_LOGGING
1079                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1080                                    "connection_read: conn %d  TLS accept error, error %d\n",
1081                                    c->c_connid, rc ));
1082 #else
1083                         Debug( LDAP_DEBUG_TRACE,
1084                                 "connection_read(%d): TLS accept error "
1085                                 "error=%d id=%ld, closing\n",
1086                                 s, rc, c->c_connid );
1087 #endif
1088                         c->c_needs_tls_accept = 0;
1089                         /* connections_mutex and c_mutex are locked */
1090                         connection_closing( c );
1091
1092 #if 0
1093                         /* Drain input before close, to allow SSL error codes
1094                          * to propagate to client. */
1095                         FD_ZERO(&rfd);
1096                         FD_SET(s, &rfd);
1097                         for (rc=1; rc>0;)
1098                         {
1099                             tv.tv_sec = 1;
1100                             tv.tv_usec = 0;
1101                             rc = select(s+1, &rfd, NULL, NULL, &tv);
1102                             if (rc == 1)
1103                                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
1104                                     NULL);
1105                         }
1106 #endif
1107                         connection_close( c );
1108
1109                 } else if ( rc == 0 ) {
1110                         void *ssl;
1111                         char *authid;
1112
1113                         c->c_needs_tls_accept = 0;
1114
1115                         /* we need to let SASL know */
1116                         ssl = (void *)ldap_pvt_tls_sb_ctx( c->c_sb );
1117
1118                         c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1119                         if( c->c_tls_ssf > c->c_ssf ) {
1120                                 c->c_ssf = c->c_tls_ssf;
1121                         }
1122
1123                         authid = (char *)ldap_pvt_tls_get_peer( ssl );
1124                         slap_sasl_external( c, c->c_tls_ssf, authid );
1125                 }
1126                 connection_return( c );
1127                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1128                 return 0;
1129         }
1130 #endif
1131
1132 #ifdef HAVE_CYRUS_SASL
1133         if ( c->c_sasl_layers ) {
1134                 c->c_sasl_layers = 0;
1135
1136                 rc = ldap_pvt_sasl_install( c->c_sb,  c->c_sasl_context );
1137
1138                 if( rc != LDAP_SUCCESS ) {
1139 #ifdef NEW_LOGGING
1140                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1141                                    "connection_read: conn %d SASL install error %d, closing\n",
1142                                    c->c_connid, rc ));
1143 #else
1144                         Debug( LDAP_DEBUG_TRACE,
1145                                 "connection_read(%d): SASL install error "
1146                                 "error=%d id=%ld, closing\n",
1147                                 s, rc, c->c_connid );
1148 #endif
1149                         /* connections_mutex and c_mutex are locked */
1150                         connection_closing( c );
1151                         connection_close( c );
1152                         connection_return( c );
1153                         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1154                         return 0;
1155                 }
1156         }
1157 #endif
1158
1159 #define CONNECTION_INPUT_LOOP 1
1160
1161 #ifdef DATA_READY_LOOP
1162         while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1163 #elif CONNECTION_INPUT_LOOP
1164         while(!rc)
1165 #endif
1166         {
1167                 /* How do we do this without getting into a busy loop ? */
1168                 rc = connection_input( c );
1169         }
1170
1171         if( rc < 0 ) {
1172 #ifdef NEW_LOGGING
1173                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1174                            "connection_read: conn %d  input error %d, closing.\n",
1175                            c->c_connid, rc ));
1176 #else
1177                 Debug( LDAP_DEBUG_TRACE,
1178                         "connection_read(%d): input error=%d id=%ld, closing.\n",
1179                         s, rc, c->c_connid );
1180 #endif
1181                 /* connections_mutex and c_mutex are locked */
1182                 connection_closing( c );
1183                 connection_close( c );
1184                 connection_return( c );
1185                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1186                 return 0;
1187         }
1188
1189         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1190                 slapd_set_read( s, 1 );
1191         }
1192
1193         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1194                 slapd_set_write( s, 1 );
1195         }
1196
1197         connection_return( c );
1198         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1199         return 0;
1200 }
1201
1202 static int
1203 connection_input(
1204     Connection *conn
1205 )
1206 {
1207         Operation *op;
1208         ber_tag_t       tag;
1209         ber_len_t       len;
1210         ber_int_t       msgid;
1211         BerElement      *ber;
1212 #ifdef LDAP_CONNECTIONLESS
1213         Sockaddr        peeraddr;
1214         char            *cdn = NULL;
1215 #endif
1216
1217         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1218             == NULL ) {
1219 #ifdef NEW_LOGGING
1220                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1221                            "connection_input: conn %d  ber_alloc failed.\n", conn->c_connid ));
1222 #else
1223                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1224 #endif
1225                 return -1;
1226         }
1227
1228         errno = 0;
1229
1230 #ifdef LDAP_CONNECTIONLESS
1231         if (conn->c_is_udp)
1232         {
1233                 char    peername[sizeof("IP=255.255.255.255:65336")];
1234                 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1235                         sizeof(struct sockaddr));
1236                 if (len != sizeof(struct sockaddr))
1237                         return 1;
1238                 sprintf( peername, "IP=%s:%d",
1239                         inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1240                         (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1241                 Statslog( LDAP_DEBUG_STATS,
1242                             "conn=%ld UDP request from %s (%s) accepted.\n",
1243                             conn->c_connid, peername,
1244                             conn->c_sock_name, 0, 0 );
1245         }
1246 #endif
1247         tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1248         if ( tag != LDAP_TAG_MESSAGE ) {
1249                 int err = errno;
1250                 ber_socket_t    sd;
1251
1252                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1253
1254 #ifdef NEW_LOGGING
1255                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1256                            "connection_input: conn %d  ber_get_next failed, errno %d (%s).\n",
1257                            conn->c_connid, err, sock_errstr(err) ));
1258 #else
1259                 Debug( LDAP_DEBUG_TRACE,
1260                         "ber_get_next on fd %d failed errno=%d (%s)\n",
1261                         sd, err, sock_errstr(err) );
1262 #endif
1263                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1264                         /* log, close and send error */
1265                         ber_free( conn->c_currentber, 1 );
1266                         conn->c_currentber = NULL;
1267
1268                         return -2;
1269                 }
1270                 return 1;
1271         }
1272
1273         ber = conn->c_currentber;
1274         conn->c_currentber = NULL;
1275
1276         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1277                 /* log, close and send error */
1278 #ifdef NEW_LOGGING
1279                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1280                            "connection_input: conn %d  ber_get_int returns 0x%lx.\n",
1281                            conn->c_connid, tag ));
1282 #else
1283                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1284                     0 );
1285 #endif
1286                 ber_free( ber, 1 );
1287                 return -1;
1288         }
1289
1290         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1291                 /* log, close and send error */
1292 #ifdef NEW_LOGGING
1293                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1294                            "connection_input: conn %d  ber_peek_tag returns 0x%lx.\n",
1295                            conn->c_connid, tag ));
1296 #else
1297                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1298                     0 );
1299 #endif
1300                 ber_free( ber, 1 );
1301
1302                 return -1;
1303         }
1304
1305 #ifdef LDAP_CONNECTIONLESS
1306         if (conn->c_is_udp) {
1307                 if (tag == LBER_OCTETSTRING) {
1308                         ber_get_stringa( ber, &cdn );
1309                         tag = ber_peek_tag(ber, &len);
1310                 }
1311                 if (tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH) {
1312 #ifdef NEW_LOGGING
1313                     LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1314                                "connection_input: conn %d  invalid req for UDP 0x%lx.\n",
1315                                conn->c_connid, tag ));
1316 #else
1317                     Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1318                         0 );
1319 #endif
1320                     ber_free( ber, 1 );
1321                     return 0;
1322                 }
1323         }
1324 #endif
1325         if(tag == LDAP_REQ_BIND) {
1326                 /* immediately abandon all exiting operations upon BIND */
1327                 connection_abandon( conn );
1328         }
1329
1330         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1331
1332 #ifdef LDAP_CONNECTIONLESS
1333         op->o_peeraddr = peeraddr;
1334         if (cdn) {
1335             op->o_dn = cdn;
1336             op->o_protocol = LDAP_VERSION2;
1337         }
1338 #endif
1339         if ( conn->c_conn_state == SLAP_C_BINDING
1340                 || conn->c_conn_state == SLAP_C_CLOSING )
1341         {
1342 #ifdef NEW_LOGGING
1343                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1344                            "connection_input: conn %d  deferring operation\n",
1345                            conn->c_connid ));
1346 #else
1347                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1348 #endif
1349                 conn->c_n_ops_pending++;
1350                 slap_op_add( &conn->c_pending_ops, op );
1351
1352         } else {
1353                 conn->c_n_ops_executing++;
1354                 connection_op_activate( conn, op );
1355         }
1356
1357 #ifdef NO_THREADS
1358         if ( conn->c_struct_state != SLAP_C_USED ) {
1359                 /* connection must have got closed underneath us */
1360                 return 1;
1361         }
1362 #endif
1363         assert( conn->c_struct_state == SLAP_C_USED );
1364
1365         return 0;
1366 }
1367
1368 static int
1369 connection_resched( Connection *conn )
1370 {
1371         Operation *op;
1372
1373         if( conn->c_conn_state == SLAP_C_CLOSING ) {
1374                 int rc;
1375                 ber_socket_t    sd;
1376                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1377
1378                 /* us trylock to avoid possible deadlock */
1379                 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1380
1381                 if( rc ) {
1382 #ifdef NEW_LOGGING
1383                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1384                                    "connection_resched: conn %d  reaquiring locks.\n",
1385                                    conn->c_connid ));
1386 #else
1387                         Debug( LDAP_DEBUG_TRACE,
1388                                 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1389                                 conn->c_connid, sd, 0 );
1390 #endif
1391                         /*
1392                          * reaquire locks in the right order...
1393                          * this may allow another thread to close this connection,
1394                          * so recheck state below.
1395                          */
1396                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1397                         ldap_pvt_thread_mutex_lock( &connections_mutex );
1398                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1399                 }
1400
1401                 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1402 #ifdef NEW_LOGGING
1403                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1404                                    "connection_resched: conn %d  closed by other thread.\n",
1405                                    conn->c_connid ));
1406 #else
1407                         Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1408                                 "closed by other thread conn=%ld sd=%d\n",
1409                                 conn->c_connid, sd, 0 );
1410 #endif
1411                 } else {
1412 #ifdef NEW_LOGGING
1413                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1414                                    "connection_resched: conn %d  attempting closing.\n",
1415                                    conn->c_connid ));
1416 #else
1417                         Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1418                                 "attempting closing conn=%ld sd=%d\n",
1419                                 conn->c_connid, sd, 0 );
1420 #endif
1421                         connection_close( conn );
1422                 }
1423
1424                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1425                 return 0;
1426         }
1427
1428         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1429                 /* other states need different handling */
1430                 return 0;
1431         }
1432
1433         for( op = slap_op_pop( &conn->c_pending_ops );
1434                 op != NULL;
1435                 op = slap_op_pop( &conn->c_pending_ops ) )
1436         {
1437                 /* pending operations should not be marked for abandonment */
1438                 assert(!op->o_abandon);
1439
1440                 conn->c_n_ops_pending--;
1441                 conn->c_n_ops_executing++;
1442
1443                 connection_op_activate( conn, op );
1444
1445                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1446                         break;
1447                 }
1448         }
1449         return 0;
1450 }
1451
1452 static int connection_op_activate( Connection *conn, Operation *op )
1453 {
1454         struct co_arg *arg;
1455         int status;
1456         ber_tag_t tag = op->o_tag;
1457
1458         if(tag == LDAP_REQ_BIND) {
1459                 conn->c_conn_state = SLAP_C_BINDING;
1460         }
1461
1462         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1463         arg->co_conn = conn;
1464         arg->co_op = op;
1465
1466         if (!arg->co_op->o_dn) {
1467             arg->co_op->o_authz = conn->c_authz;
1468             arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1469             arg->co_op->o_ndn = ch_strdup( conn->c_ndn != NULL ? conn->c_ndn : "" );
1470         }
1471         arg->co_op->o_authtype = conn->c_authtype;
1472         arg->co_op->o_authmech = conn->c_authmech != NULL
1473                 ?  ch_strdup( conn->c_authmech ) : NULL;
1474         
1475         if (!arg->co_op->o_protocol) {
1476             arg->co_op->o_protocol = conn->c_protocol
1477                 ? conn->c_protocol : LDAP_VERSION3;
1478         }
1479         arg->co_op->o_connid = conn->c_connid;
1480
1481         slap_op_add( &conn->c_ops, arg->co_op );
1482
1483         status = ldap_pvt_thread_pool_submit( &connection_pool,
1484                 connection_operation, (void *) arg );
1485
1486         if ( status != 0 ) {
1487 #ifdef NEW_LOGGING
1488                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1489                            "connection_op_activate: conn %d      thread pool submit failed.\n",
1490                            conn->c_connid ));
1491 #else
1492                 Debug( LDAP_DEBUG_ANY,
1493                 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1494 #endif
1495                 /* should move op to pending list */
1496         }
1497
1498         return status;
1499 }
1500
1501 int connection_write(ber_socket_t s)
1502 {
1503         Connection *c;
1504         assert( connections != NULL );
1505
1506         ldap_pvt_thread_mutex_lock( &connections_mutex );
1507
1508         c = connection_get( s );
1509
1510         slapd_clr_write( s, 0);
1511
1512         if( c == NULL ) {
1513 #ifdef NEW_LOGGING
1514                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1515                            "connection_write: sock %ld  no connection!\n",(long)s));
1516 #else
1517                 Debug( LDAP_DEBUG_ANY,
1518                         "connection_write(%ld): no connection!\n",
1519                         (long) s, 0, 0 );
1520 #endif
1521                 slapd_remove(s, 0);
1522                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1523                 return -1;
1524         }
1525
1526         c->c_n_write++;
1527
1528 #ifdef NEW_LOGGING
1529         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1530                    "connection_write conn %d  waking output.\n",
1531                    c->c_connid ));
1532 #else
1533         Debug( LDAP_DEBUG_TRACE,
1534                 "connection_write(%d): waking output for id=%ld\n",
1535                 s, c->c_connid, 0 );
1536 #endif
1537         ldap_pvt_thread_cond_signal( &c->c_write_cv );
1538
1539         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1540                 slapd_set_read( s, 1 );
1541         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1542                 slapd_set_write( s, 1 );
1543         connection_return( c );
1544         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1545         return 0;
1546 }
1547
1548
1549 /*
1550  * Create client side and server side connection structures, connected to
1551  * one another, for the front end to use for searches on arbitrary back ends.
1552  */
1553
1554 int connection_internal_open( Connection **conn, LDAP **ldp, const char *id )
1555 {
1556         int rc;
1557         ber_socket_t fd[2] = {-1,-1};
1558         Operation *op;
1559
1560         *conn=NULL;
1561         *ldp=NULL;
1562
1563         rc = lutil_pair( fd );
1564         if( rc == -1 ) {
1565                 return LDAP_OTHER;
1566         }
1567
1568         rc = connection_init( fd[1], "INT", "localhost", 
1569       "localhost:0", "localhost:00", 0, 256, id );
1570         if( rc < 0 ) {
1571                 tcp_close( fd[0] );
1572                 tcp_close( fd[1] );
1573                 return LDAP_OTHER;
1574         }
1575         slapd_add_internal( fd[1] );
1576
1577         /* A search operation, number 0 */
1578         op = slap_op_alloc( NULL, 0, LDAP_REQ_SEARCH, 0);
1579         op->o_ndn = ch_strdup( id );
1580         op->o_protocol = LDAP_VERSION3;
1581
1582         (*conn) = connection_get( fd[1] );
1583         (*conn)->c_ops = op;
1584     (*conn)->c_conn_state = SLAP_C_ACTIVE;
1585
1586
1587         /* Create the client side of the connection */
1588         rc = ldap_open_internal_connection( ldp, &(fd[0]) );
1589         if( rc != LDAP_SUCCESS ) {
1590                 tcp_close( fd[0] );
1591                 return LDAP_OTHER;
1592         }
1593
1594         /* The connection_get() will have locked the connection's mutex */
1595         ldap_pvt_thread_mutex_unlock(  &((*conn)->c_mutex) );
1596
1597         return LDAP_SUCCESS;
1598 }
1599
1600
1601 void connection_internal_close( Connection *conn )
1602 {
1603         Operation *op = conn->c_ops;
1604
1605         slap_op_remove( &conn->c_ops, op );
1606         slap_op_free( op );
1607         connection_closing( conn );
1608         connection_close( conn );
1609 }