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