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