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