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