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