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