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