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