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