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