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