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