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