]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
71d6a35b5737bff7f6902429735d9e8ad614a883
[openldap] / servers / slapd / connection.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29 #ifdef HAVE_LIMITS_H
30 #include <limits.h>
31 #endif
32
33 #include <ac/socket.h>
34 #include <ac/errno.h>
35 #include <ac/string.h>
36 #include <ac/time.h>
37 #include <ac/unistd.h>
38
39 #include "lutil.h"
40 #include "slap.h"
41
42 #ifdef LDAP_SLAPI
43 #include "slapi/slapi.h"
44 #endif
45
46 #ifdef SLAP_MULTI_CONN_ARRAY
47 /* for Multiple Connection Arrary (MCA) Support */
48 static ldap_pvt_thread_mutex_t* connections_mutex;
49 static Connection **connections = NULL;
50
51 /* set to the number of processors (round up to a power of 2) */
52 #       define NUM_CONNECTION_ARRAY 4
53
54 /* partition the array in a modulo manner */
55 #       define MCA_conn_array_id(fd)            ((int)(fd)%NUM_CONNECTION_ARRAY)
56 #       define MCA_conn_array_element_id(fd)    ((int)(fd)/NUM_CONNECTION_ARRAY)
57 #       define MCA_ARRAY_SIZE                   ((int)(MCA_conn_array_element_id(dtblsize) + (MCA_conn_array_id(dtblsize) ? 1 : 0)))
58 #       define MCA_conn_check(fd)               (dtblsize > 0 && (fd) >= 0 && (fd) < (MCA_ARRAY_SIZE*NUM_CONNECTION_ARRAY))
59 #       define MCA_GET_CONNECTION(fd) (&(connections[MCA_conn_array_id(fd)]) \
60                 [MCA_conn_array_element_id(fd)])
61 #       define MCA_GET_CONN_MUTEX(fd) (&connections_mutex[MCA_conn_array_id(fd)])
62
63 #else
64 /* protected by connections_mutex */
65 static ldap_pvt_thread_mutex_t connections_mutex;
66 static Connection *connections = NULL;
67
68 #       define MCA_conn_check(fd)               (dtblsize > 0 && (fd) < dtblsize)
69 #       define MCA_GET_CONNECTION(fd) (&connections[s])
70 #       define MCA_GET_CONN_MUTEX(fd) (&connections_mutex)
71 #endif
72
73 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
74 static unsigned long conn_nextid = 0;
75
76 static const char conn_lost_str[] = "connection lost";
77
78 /* structure state (protected by connections_mutex) */
79 #define SLAP_C_UNINITIALIZED    0x00    /* MUST BE ZERO (0) */
80 #define SLAP_C_UNUSED                   0x01
81 #define SLAP_C_USED                             0x02
82
83 /* connection state (protected by c_mutex ) */
84 #define SLAP_C_INVALID                  0x00    /* MUST BE ZERO (0) */
85 #define SLAP_C_INACTIVE                 0x01    /* zero threads */
86 #define SLAP_C_ACTIVE                   0x02    /* one or more threads */
87 #define SLAP_C_BINDING                  0x03    /* binding */
88 #define SLAP_C_CLOSING                  0x04    /* closing */
89 #define SLAP_C_CLIENT                   0x05    /* outbound client conn */
90
91 const char *
92 connection_state2str( int state )
93 {
94         switch( state ) {
95         case SLAP_C_INVALID:    return "!";
96         case SLAP_C_INACTIVE:   return "|";
97         case SLAP_C_ACTIVE:             return "";
98         case SLAP_C_BINDING:    return "B";
99         case SLAP_C_CLOSING:    return "C";
100         case SLAP_C_CLIENT:             return "L";
101         }
102
103         return "?";
104 }
105
106 static Connection* connection_get( ber_socket_t s );
107
108 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
109
110 typedef struct conn_readinfo {
111         Operation *op;
112         ldap_pvt_thread_start_t *func;
113         void *arg;
114         int nullop;
115 } conn_readinfo;
116
117 static int connection_input( Connection *c, conn_readinfo *cri );
118 #else
119 static int connection_input( Connection *c );
120 #endif
121 static void connection_close( Connection *c );
122
123 static int connection_op_activate( Operation *op );
124 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
125 static void connection_op_queue( Operation *op );
126 #endif
127 static int connection_resched( Connection *conn );
128 static void connection_abandon( Connection *conn );
129 static void connection_destroy( Connection *c );
130
131 static ldap_pvt_thread_start_t connection_operation;
132
133 /*
134  * Initialize connection management infrastructure.
135  */
136 int connections_init(void)
137 #ifdef SLAP_MULTI_CONN_ARRAY
138 {
139         int             i, j;
140         Connection*     conn;
141
142         assert( connections == NULL );
143
144         if( connections != NULL) {
145                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
146                         0, 0, 0 );
147                 return -1;
148         }
149
150         connections_mutex = (ldap_pvt_thread_mutex_t*) ch_calloc(
151                 NUM_CONNECTION_ARRAY, sizeof(ldap_pvt_thread_mutex_t) );
152         if( connections_mutex == NULL ) {
153                 Debug( LDAP_DEBUG_ANY, "connections_init: "
154                         "allocation of connection mutexes failed\n", 0, 0, 0 );
155                 return -1;
156         }
157
158         connections = (Connection**) ch_calloc(
159                 NUM_CONNECTION_ARRAY, sizeof(Connection*));
160         if( connections == NULL ) {
161                 Debug( LDAP_DEBUG_ANY, "connections_init: "
162                         "allocation of connection[%d] failed\n", 0, 0, 0 );
163                 return -1;
164         }
165
166         for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
167                 ldap_pvt_thread_mutex_init( connections_mutex+i );
168                 connections[i] = (Connection*) ch_calloc(
169                         MCA_ARRAY_SIZE, sizeof(Connection) );
170                 if( connections[i] == NULL ) {
171                         Debug( LDAP_DEBUG_ANY, "connections_init: "
172                                 "allocation (%d*%ld) of connection array[%d] failed\n",
173                                 dtblsize, (long) sizeof(Connection), i );
174                         return -1;
175                 }
176         }
177
178         /* should check return of every call */
179         ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
180
181         assert( connections[0]->c_struct_state == SLAP_C_UNINITIALIZED );
182         assert( connections[NUM_CONNECTION_ARRAY-1]->c_struct_state ==
183                 SLAP_C_UNINITIALIZED );
184
185         for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
186                 conn = connections[i];
187                 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
188                         conn[j].c_conn_idx = j;
189                 }
190         }
191
192         /*
193          * per entry initialization of the Connection array initialization
194          * will be done by connection_init()
195          */ 
196
197         return 0;
198 }
199 #else
200 {
201         int i;
202
203         assert( connections == NULL );
204
205         if( connections != NULL) {
206                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
207                         0, 0, 0 );
208                 return -1;
209         }
210
211         /* should check return of every call */
212         ldap_pvt_thread_mutex_init( &connections_mutex );
213         ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
214
215         connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
216
217         if( connections == NULL ) {
218                 Debug( LDAP_DEBUG_ANY, "connections_init: "
219                         "allocation (%d*%ld) of connection array failed\n",
220                         dtblsize, (long) sizeof(Connection), 0 );
221                 return -1;
222         }
223
224         assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
225         assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
226
227         for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
228
229         /*
230          * per entry initialization of the Connection array initialization
231          * will be done by connection_init()
232          */ 
233
234         return 0;
235 }
236 #endif
237
238 /*
239  * Destroy connection management infrastructure.
240  */
241
242 int connections_destroy(void)
243 #ifdef SLAP_MULTI_CONN_ARRAY
244 {
245         int i;
246         ber_socket_t j;
247
248         if( connections == NULL) {
249                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
250                         0, 0, 0 );
251                 return -1;
252         }
253
254     for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
255                 Connection* conn = connections[i];
256                 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
257                         if( conn[j].c_struct_state != SLAP_C_UNINITIALIZED ) {
258                                 ber_sockbuf_free( conn[j].c_sb );
259                                 ldap_pvt_thread_mutex_destroy( &conn[j].c_mutex );
260                                 ldap_pvt_thread_mutex_destroy( &conn[j].c_write_mutex );
261                                 ldap_pvt_thread_cond_destroy( &conn[j].c_write_cv );
262 #ifdef LDAP_SLAPI
263                                 /* FIX ME!! */
264                                 if ( slapi_plugins_used ) {
265                                         slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
266                                                 &connections[i] );
267                                 }
268 #endif
269                         }
270                 }
271         }
272
273         for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
274                 free( connections[i] );
275                 connections[i] = NULL;
276                 ldap_pvt_thread_mutex_destroy( &connections_mutex[i] );
277         }
278
279         free( connections );
280         free( connections_mutex );
281
282         ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
283
284         return 0;
285
286 }
287 #else
288 {
289         ber_socket_t i;
290
291         /* should check return of every call */
292
293         if( connections == NULL) {
294                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
295                         0, 0, 0 );
296                 return -1;
297         }
298
299         for ( i = 0; i < dtblsize; i++ ) {
300                 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
301                         ber_sockbuf_free( connections[i].c_sb );
302                         ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
303                         ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
304                         ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
305 #ifdef LDAP_SLAPI
306                         if ( slapi_plugins_used ) {
307                                 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
308                                         &connections[i] );
309                         }
310 #endif
311                 }
312         }
313
314         free( connections );
315         connections = NULL;
316
317         ldap_pvt_thread_mutex_destroy( &connections_mutex );
318         ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
319         return 0;
320 }
321 #endif
322
323 /*
324  * shutdown all connections
325  */
326 int connections_shutdown(void)
327 #ifdef SLAP_MULTI_CONN_ARRAY
328 {
329         int i;
330         ber_socket_t j;
331
332         for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
333                 Connection* conn = connections[i];
334                 ldap_pvt_thread_mutex_lock( &connections_mutex[i] );
335                 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
336                         if( conn[j].c_struct_state != SLAP_C_USED ) {
337                                 continue;
338                         }
339                         /* give persistent clients a chance to cleanup */
340                         if( conn[j].c_conn_state == SLAP_C_CLIENT ) {
341                                 ldap_pvt_thread_pool_submit( &connection_pool,
342                                 conn[j].c_clientfunc, conn[j].c_clientarg );
343                                 continue;
344                         }
345
346                         ldap_pvt_thread_mutex_lock( &conn[j].c_mutex );
347                         /* connections_mutex and c_mutex are locked */
348                         connection_closing( &conn[j], "connection shutdown" );
349                         connection_close( &conn[j] );
350                         ldap_pvt_thread_mutex_unlock( &conn[j].c_mutex );
351                 }
352
353                 ldap_pvt_thread_mutex_unlock( &connections_mutex[i] );
354         }
355
356         return 0;
357
358 }
359 #else
360 {
361         ber_socket_t i;
362
363         ldap_pvt_thread_mutex_lock( &connections_mutex );
364
365         for ( i = 0; i < dtblsize; i++ ) {
366                 if( connections[i].c_struct_state != SLAP_C_USED ) {
367                         continue;
368                 }
369                 /* give persistent clients a chance to cleanup */
370                 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
371                         ldap_pvt_thread_pool_submit( &connection_pool,
372                         connections[i].c_clientfunc, connections[i].c_clientarg );
373                         continue;
374                 }
375
376                 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
377
378                 /* connections_mutex and c_mutex are locked */
379                 connection_closing( &connections[i], "slapd shutdown" );
380                 connection_close( &connections[i] );
381
382                 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
383         }
384
385         ldap_pvt_thread_mutex_unlock( &connections_mutex );
386
387         return 0;
388 }
389 #endif
390
391 /*
392  * Timeout idle connections.
393  */
394 int connections_timeout_idle(time_t now)
395 {
396         int i = 0;
397         int connindex;
398         Connection* c;
399
400         for( c = connection_first( &connindex );
401                 c != NULL;
402                 c = connection_next( c, &connindex ) )
403         {
404                 /* Don't timeout a slow-running request or a persistent
405                  * outbound connection */
406                 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
407                         continue;
408                 }
409
410                 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
411                         /* close it */
412                         connection_closing( c, "idletimeout" );
413                         connection_close( c );
414                         i++;
415                 }
416         }
417         connection_done( c );
418
419         return i;
420 }
421
422 static Connection* connection_get( ber_socket_t s )
423 {
424         /* connections_mutex should be locked by caller */
425
426         Connection *c;
427
428         Debug( LDAP_DEBUG_ARGS,
429                 "connection_get(%ld)\n",
430                 (long) s, 0, 0 );
431
432         assert( connections != NULL );
433
434         if(s == AC_SOCKET_INVALID) return NULL;
435
436 #ifndef HAVE_WINSOCK
437         assert( MCA_conn_check( s ) );
438         c = MCA_GET_CONNECTION(s);
439
440         assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
441
442 #else
443         c = NULL;
444         {
445                 ber_socket_t i, sd;
446
447                 for(i=0; i<dtblsize; i++) {
448                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
449                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
450                                 assert( connections[i].c_sb == 0 );
451                                 break;
452                         }
453
454                         ber_sockbuf_ctrl( connections[i].c_sb,
455                                 LBER_SB_OPT_GET_FD, &sd );
456
457                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
458                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
459                                 assert( sd == AC_SOCKET_INVALID );
460                                 continue;
461                         }
462
463                         /* state can actually change from used -> unused by resched,
464                          * so don't assert details here.
465                          */
466
467                         if( sd == s ) {
468                                 c = &connections[i];
469                                 break;
470                         }
471                 }
472         }
473 #endif
474
475         if( c != NULL ) {
476                 ber_socket_t    sd;
477
478                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
479
480                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
481                 if( c->c_struct_state != SLAP_C_USED ) {
482                         /* connection must have been closed due to resched */
483
484                         assert( c->c_conn_state == SLAP_C_INVALID );
485                         assert( sd == AC_SOCKET_INVALID );
486
487                         Debug( LDAP_DEBUG_TRACE,
488                                 "connection_get(%d): connection not used\n",
489                                 s, 0, 0 );
490
491                         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
492                         return NULL;
493                 }
494
495                 Debug( LDAP_DEBUG_TRACE,
496                         "connection_get(%d): got connid=%lu\n",
497                         s, c->c_connid, 0 );
498
499                 c->c_n_get++;
500
501                 assert( c->c_struct_state == SLAP_C_USED );
502                 assert( c->c_conn_state != SLAP_C_INVALID );
503                 assert( sd != AC_SOCKET_INVALID );
504
505 #ifndef SLAPD_MONITOR
506                 if ( global_idletimeout > 0 )
507 #endif /* ! SLAPD_MONITOR */
508                 {
509                         c->c_activitytime = slap_get_time();
510                 }
511         }
512
513         return c;
514 }
515
516 static void connection_return( Connection *c )
517 {
518         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
519 }
520
521 long connection_init(
522         ber_socket_t s,
523         Listener *listener,
524         const char* dnsname,
525         const char* peername,
526         int flags,
527         slap_ssf_t ssf,
528         struct berval *authid )
529 {
530         unsigned long id;
531         Connection *c;
532
533         assert( connections != NULL );
534
535         assert( listener != NULL );
536         assert( dnsname != NULL );
537         assert( peername != NULL );
538
539 #ifndef HAVE_TLS
540         assert( flags != CONN_IS_TLS );
541 #endif
542
543         if( s == AC_SOCKET_INVALID ) {
544                 Debug( LDAP_DEBUG_ANY,
545                         "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
546                 return -1;
547         }
548
549         assert( s >= 0 );
550 #ifndef HAVE_WINSOCK
551         assert( s < dtblsize );
552 #endif
553
554         ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
555
556 #ifndef HAVE_WINSOCK
557         assert( MCA_conn_check( s ) );
558         c = MCA_GET_CONNECTION(s);
559 #else
560         {
561                 ber_socket_t i;
562                 c = NULL;
563
564                 for( i=0; i < dtblsize; i++) {
565                         ber_socket_t    sd;
566
567                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
568                                 assert( connections[i].c_sb == 0 );
569                                 c = &connections[i];
570                                 break;
571                         }
572
573                         sd = AC_SOCKET_INVALID;
574                         if (connections[i].c_sb != NULL) {
575                                 ber_sockbuf_ctrl( connections[i].c_sb,
576                                         LBER_SB_OPT_GET_FD, &sd );
577                         }
578
579                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
580                                 assert( sd == AC_SOCKET_INVALID );
581                                 c = &connections[i];
582                                 break;
583                         }
584
585                         if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
586
587                         assert( connections[i].c_struct_state == SLAP_C_USED );
588                         assert( connections[i].c_conn_state != SLAP_C_INVALID );
589                         assert( sd != AC_SOCKET_INVALID );
590                 }
591
592                 if( c == NULL ) {
593                         Debug( LDAP_DEBUG_ANY,
594                                 "connection_init(%d): connection table full "
595                                 "(%d/%d)\n", s, i, dtblsize);
596                         ldap_pvt_thread_mutex_unlock( &connections_mutex );
597                         return -1;
598                 }
599         }
600 #endif
601
602         assert( c != NULL );
603
604         if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
605                 c->c_send_ldap_result = slap_send_ldap_result;
606                 c->c_send_search_entry = slap_send_search_entry;
607                 c->c_send_search_reference = slap_send_search_reference;
608                 c->c_send_ldap_extended = slap_send_ldap_extended;
609                 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
610
611                 BER_BVZERO( &c->c_authmech );
612                 BER_BVZERO( &c->c_dn );
613                 BER_BVZERO( &c->c_ndn );
614
615                 c->c_listener = NULL;
616                 BER_BVZERO( &c->c_peer_domain );
617                 BER_BVZERO( &c->c_peer_name );
618
619                 LDAP_STAILQ_INIT(&c->c_ops);
620                 LDAP_STAILQ_INIT(&c->c_pending_ops);
621
622 #ifdef LDAP_X_TXN
623                 c->c_txn = CONN_TXN_INACTIVE;
624                 c->c_txn_backend = NULL;
625                 LDAP_STAILQ_INIT(&c->c_txn_ops);
626 #endif
627
628                 BER_BVZERO( &c->c_sasl_bind_mech );
629                 c->c_sasl_done = 0;
630                 c->c_sasl_authctx = NULL;
631                 c->c_sasl_sockctx = NULL;
632                 c->c_sasl_extra = NULL;
633                 c->c_sasl_bindop = NULL;
634
635                 c->c_sb = ber_sockbuf_alloc( );
636
637                 {
638                         ber_len_t max = sockbuf_max_incoming;
639                         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
640                 }
641
642                 c->c_currentber = NULL;
643
644                 /* should check status of thread calls */
645                 ldap_pvt_thread_mutex_init( &c->c_mutex );
646                 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
647                 ldap_pvt_thread_cond_init( &c->c_write_cv );
648
649 #ifdef LDAP_SLAPI
650                 if ( slapi_plugins_used ) {
651                         slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
652                 }
653 #endif
654
655                 c->c_struct_state = SLAP_C_UNUSED;
656         }
657
658         ldap_pvt_thread_mutex_lock( &c->c_mutex );
659
660         assert( c->c_struct_state == SLAP_C_UNUSED );
661         assert( BER_BVISNULL( &c->c_authmech ) );
662         assert( BER_BVISNULL( &c->c_dn ) );
663         assert( BER_BVISNULL( &c->c_ndn ) );
664         assert( c->c_listener == NULL );
665         assert( BER_BVISNULL( &c->c_peer_domain ) );
666         assert( BER_BVISNULL( &c->c_peer_name ) );
667         assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
668         assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
669 #ifdef LDAP_X_TXN
670         assert( c->c_txn == CONN_TXN_INACTIVE );
671         assert( c->c_txn_backend == NULL );
672         assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
673 #endif
674         assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
675         assert( c->c_sasl_done == 0 );
676         assert( c->c_sasl_authctx == NULL );
677         assert( c->c_sasl_sockctx == NULL );
678         assert( c->c_sasl_extra == NULL );
679         assert( c->c_sasl_bindop == NULL );
680         assert( c->c_currentber == NULL );
681         assert( c->c_writewaiter == 0);
682
683         c->c_listener = listener;
684
685         if ( flags == CONN_IS_CLIENT ) {
686                 c->c_conn_state = SLAP_C_CLIENT;
687                 c->c_struct_state = SLAP_C_USED;
688                 c->c_close_reason = "?";                        /* should never be needed */
689                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
690                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
691                 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
692
693                 return 0;
694         }
695
696         ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
697         ber_str2bv( peername, 0, 1, &c->c_peer_name );
698
699         c->c_n_ops_received = 0;
700         c->c_n_ops_executing = 0;
701         c->c_n_ops_pending = 0;
702         c->c_n_ops_completed = 0;
703
704         c->c_n_get = 0;
705         c->c_n_read = 0;
706         c->c_n_write = 0;
707
708         /* set to zero until bind, implies LDAP_VERSION3 */
709         c->c_protocol = 0;
710
711 #ifndef SLAPD_MONITOR
712         if ( global_idletimeout > 0 )
713 #endif /* ! SLAPD_MONITOR */
714         {
715                 c->c_activitytime = c->c_starttime = slap_get_time();
716         }
717
718 #ifdef LDAP_CONNECTIONLESS
719         c->c_is_udp = 0;
720         if( flags == CONN_IS_UDP ) {
721                 c->c_is_udp = 1;
722 #ifdef LDAP_DEBUG
723                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
724                         LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
725 #endif
726                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
727                         LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
728                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
729                         LBER_SBIOD_LEVEL_PROVIDER, NULL );
730         } else
731 #endif
732         {
733 #ifdef LDAP_DEBUG
734                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
735                         LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
736 #endif
737                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
738                         LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
739         }
740
741 #ifdef LDAP_DEBUG
742         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
743                 INT_MAX, (void*)"ldap_" );
744 #endif
745
746         if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
747                 c /* non-NULL */ ) < 0 )
748         {
749                 Debug( LDAP_DEBUG_ANY,
750                         "connection_init(%d, %s): set nonblocking failed\n",
751                         s, c->c_peer_name.bv_val, 0 );
752         }
753
754         ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
755         id = c->c_connid = conn_nextid++;
756         ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
757
758         c->c_conn_state = SLAP_C_INACTIVE;
759         c->c_struct_state = SLAP_C_USED;
760         c->c_close_reason = "?";                        /* should never be needed */
761
762         c->c_ssf = c->c_transport_ssf = ssf;
763         c->c_tls_ssf = 0;
764
765 #ifdef HAVE_TLS
766         if ( flags == CONN_IS_TLS ) {
767                 c->c_is_tls = 1;
768                 c->c_needs_tls_accept = 1;
769         } else {
770                 c->c_is_tls = 0;
771                 c->c_needs_tls_accept = 0;
772         }
773 #endif
774
775         slap_sasl_open( c, 0 );
776         slap_sasl_external( c, ssf, authid );
777
778         slapd_add_internal( s, 1 );
779         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
780         ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
781
782         backend_connection_init(c);
783
784         return id;
785 }
786
787 void connection2anonymous( Connection *c )
788 {
789         assert( connections != NULL );
790         assert( c != NULL );
791
792         {
793                 ber_len_t max = sockbuf_max_incoming;
794                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
795         }
796
797         if ( !BER_BVISNULL( &c->c_authmech ) ) {
798                 ch_free(c->c_authmech.bv_val);
799         }
800         BER_BVZERO( &c->c_authmech );
801
802         if ( !BER_BVISNULL( &c->c_dn ) ) {
803                 ch_free(c->c_dn.bv_val);
804         }
805         BER_BVZERO( &c->c_dn );
806
807         if ( !BER_BVISNULL( &c->c_ndn ) ) {
808                 ch_free(c->c_ndn.bv_val);
809         }
810         BER_BVZERO( &c->c_ndn );
811
812         if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
813                 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
814         }
815         BER_BVZERO( &c->c_sasl_authz_dn );
816
817         c->c_authz_backend = NULL;
818 }
819
820 static void
821 connection_destroy( Connection *c )
822 {
823         /* note: connections_mutex should be locked by caller */
824         ber_socket_t    sd;
825         unsigned long   connid;
826         const char              *close_reason;
827
828         assert( connections != NULL );
829         assert( c != NULL );
830         assert( c->c_struct_state != SLAP_C_UNUSED );
831         assert( c->c_conn_state != SLAP_C_INVALID );
832         assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
833         assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
834 #ifdef LDAP_X_TXN
835         assert( c->c_txn == CONN_TXN_INACTIVE );
836         assert( c->c_txn_backend == NULL );
837         assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
838 #endif
839         assert( c->c_writewaiter == 0);
840
841         /* only for stats (print -1 as "%lu" may give unexpected results ;) */
842         connid = c->c_connid;
843         close_reason = c->c_close_reason;
844
845         backend_connection_destroy(c);
846
847         c->c_protocol = 0;
848         c->c_connid = -1;
849
850         c->c_activitytime = c->c_starttime = 0;
851
852         connection2anonymous( c );
853         c->c_listener = NULL;
854
855         if(c->c_peer_domain.bv_val != NULL) {
856                 free(c->c_peer_domain.bv_val);
857         }
858         BER_BVZERO( &c->c_peer_domain );
859         if(c->c_peer_name.bv_val != NULL) {
860                 free(c->c_peer_name.bv_val);
861         }
862         BER_BVZERO( &c->c_peer_name );
863
864         c->c_sasl_bind_in_progress = 0;
865         if(c->c_sasl_bind_mech.bv_val != NULL) {
866                 free(c->c_sasl_bind_mech.bv_val);
867         }
868         BER_BVZERO( &c->c_sasl_bind_mech );
869
870         slap_sasl_close( c );
871
872         if ( c->c_currentber != NULL ) {
873                 ber_free( c->c_currentber, 1 );
874                 c->c_currentber = NULL;
875         }
876
877         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
878         slapd_sd_lock();
879         ber_sockbuf_free( c->c_sb );
880         if ( sd != AC_SOCKET_INVALID ) {
881                 slapd_remove( sd, 1, 0, 1 );
882
883                 Statslog( LDAP_DEBUG_STATS, (close_reason
884                                                                          ? "conn=%lu fd=%ld closed (%s)\n"
885                                                                          : "conn=%lu fd=%ld closed\n"),
886                         connid, (long) sd, close_reason, 0, 0 );
887         } else {
888                 slapd_sd_unlock();
889         }
890
891         c->c_sb = ber_sockbuf_alloc( );
892
893         {
894                 ber_len_t max = sockbuf_max_incoming;
895                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
896         }
897
898         c->c_conn_state = SLAP_C_INVALID;
899         c->c_struct_state = SLAP_C_UNUSED;
900         c->c_close_reason = "?";                        /* should never be needed */
901
902 #ifdef LDAP_SLAPI
903         /* call destructors, then constructors; avoids unnecessary allocation */
904         if ( slapi_plugins_used ) {
905                 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
906         }
907 #endif
908 }
909
910 int connection_state_closing( Connection *c )
911 {
912         /* c_mutex must be locked by caller */
913
914         int state;
915         assert( c != NULL );
916         assert( c->c_struct_state == SLAP_C_USED );
917
918         state = c->c_conn_state;
919
920         assert( state != SLAP_C_INVALID );
921
922         return state == SLAP_C_CLOSING;
923 }
924
925 static void connection_abandon( Connection *c )
926 {
927         /* c_mutex must be locked by caller */
928
929         Operation *o, *next, op = {0};
930         Opheader ohdr = {0};
931         SlapReply rs = {0};
932
933         op.o_hdr = &ohdr;
934         op.o_conn = c;
935         op.o_connid = c->c_connid;
936         op.o_tag = LDAP_REQ_ABANDON;
937
938         for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
939                 next = LDAP_STAILQ_NEXT( o, o_next );
940                 op.orn_msgid = o->o_msgid;
941                 o->o_abandon = 1;
942                 op.o_bd = frontendDB;
943                 frontendDB->be_abandon( &op, &rs );
944         }
945
946 #ifdef LDAP_X_TXN
947         /* remove operations in pending transaction */
948         while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
949                 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
950                 LDAP_STAILQ_NEXT(o, o_next) = NULL;
951                 slap_op_free( o );
952         }
953
954         /* clear transaction */
955         c->c_txn_backend = NULL;
956         c->c_txn = CONN_TXN_INACTIVE;
957 #endif
958
959         /* remove pending operations */
960         while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
961                 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
962                 LDAP_STAILQ_NEXT(o, o_next) = NULL;
963                 slap_op_free( o );
964         }
965 }
966
967 void connection_closing( Connection *c, const char *why )
968 {
969         assert( connections != NULL );
970         assert( c != NULL );
971         assert( c->c_struct_state == SLAP_C_USED );
972         assert( c->c_conn_state != SLAP_C_INVALID );
973
974         /* c_mutex must be locked by caller */
975
976         if( c->c_conn_state != SLAP_C_CLOSING ) {
977                 ber_socket_t    sd;
978
979                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
980                 Debug( LDAP_DEBUG_TRACE,
981                         "connection_closing: readying conn=%lu sd=%d for close\n",
982                         c->c_connid, sd, 0 );
983                 /* update state to closing */
984                 c->c_conn_state = SLAP_C_CLOSING;
985                 c->c_close_reason = why;
986
987                 /* don't listen on this port anymore */
988                 slapd_clr_read( sd, 1 );
989
990                 /* abandon active operations */
991                 connection_abandon( c );
992
993                 /* wake write blocked operations */
994                 slapd_clr_write( sd, 1 );
995                 if ( c->c_writewaiter ) {
996                         ldap_pvt_thread_cond_signal( &c->c_write_cv );
997                         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
998                         ldap_pvt_thread_yield();
999                         ldap_pvt_thread_mutex_lock( &c->c_mutex );
1000                 }
1001
1002         } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
1003                 /* Client closed connection after doing Unbind. */
1004                 c->c_close_reason = NULL;
1005         }
1006 }
1007
1008 static void connection_close( Connection *c )
1009 {
1010         ber_socket_t    sd;
1011
1012         assert( connections != NULL );
1013         assert( c != NULL );
1014         assert( c->c_struct_state == SLAP_C_USED );
1015         assert( c->c_conn_state == SLAP_C_CLOSING );
1016
1017         /* note: connections_mutex and c_mutex should be locked by caller */
1018
1019         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
1020         if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
1021                 Debug( LDAP_DEBUG_TRACE,
1022                         "connection_close: deferring conn=%lu sd=%d\n",
1023                         c->c_connid, sd, 0 );
1024                 return;
1025         }
1026
1027         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
1028                 c->c_connid, sd, 0 );
1029         connection_destroy( c );
1030 }
1031
1032 unsigned long connections_nextid(void)
1033 {
1034         unsigned long id;
1035         assert( connections != NULL );
1036
1037         ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1038
1039         id = conn_nextid;
1040
1041         ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
1042
1043         return id;
1044 }
1045
1046 Connection* connection_first( ber_socket_t *index )
1047 {
1048 #ifdef SLAP_MULTI_CONN_ARRAY
1049         int conn_array_id;
1050 #endif
1051
1052         assert( connections != NULL );
1053         assert( index != NULL );
1054
1055 #ifdef SLAP_MULTI_CONN_ARRAY
1056         for ( conn_array_id = 0;
1057                 conn_array_id < NUM_CONNECTION_ARRAY;
1058                 conn_array_id++ )
1059         {
1060                 ldap_pvt_thread_mutex_lock( &connections_mutex[ conn_array_id ] );
1061         }
1062 #else
1063         ldap_pvt_thread_mutex_lock( &connections_mutex );
1064 #endif
1065
1066         *index = 0;
1067
1068         return connection_next(NULL, index);
1069 }
1070
1071 Connection* connection_next( Connection *c, ber_socket_t *index )
1072 #ifdef SLAP_MULTI_CONN_ARRAY
1073 {
1074         Connection* conn;
1075
1076         assert( connections != NULL );
1077         assert( index != NULL );
1078         assert( *index >= 0 && *index < MCA_ARRAY_SIZE );
1079
1080         if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1081
1082         c = NULL;
1083
1084         for(; *index < dtblsize; (*index)++) {
1085                 assert( MCA_conn_check( *index ) );
1086                 conn = MCA_GET_CONNECTION(*index);
1087                 if( conn->c_struct_state == SLAP_C_UNINITIALIZED ) {
1088                         assert( conn->c_conn_state == SLAP_C_INVALID );
1089 #ifndef HAVE_WINSOCK
1090                         continue;
1091 #else
1092                         break;
1093 #endif
1094                 }
1095
1096                 if( conn->c_struct_state == SLAP_C_USED ) {
1097                         assert( conn->c_conn_state != SLAP_C_INVALID );
1098                         c = conn;
1099                         (*index)++;
1100                         break;
1101                 }
1102
1103                 assert( conn->c_struct_state == SLAP_C_UNUSED );
1104                 assert( conn->c_conn_state == SLAP_C_INVALID );
1105         }
1106
1107         if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1108
1109         return c;
1110
1111 }
1112 #else
1113 {
1114         assert( connections != NULL );
1115         assert( index != NULL );
1116         assert( *index <= dtblsize );
1117
1118         if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1119
1120         c = NULL;
1121
1122         for(; *index < dtblsize; (*index)++) {
1123                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
1124                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1125 #ifndef HAVE_WINSOCK
1126                         continue;
1127 #else
1128                         break;
1129 #endif
1130                 }
1131
1132                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
1133                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
1134                         c = &connections[(*index)++];
1135                         break;
1136                 }
1137
1138                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
1139                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1140         }
1141
1142         if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1143         return c;
1144 }
1145 #endif
1146
1147 void connection_done( Connection *c )
1148 {
1149 #ifdef SLAP_MULTI_CONN_ARRAY
1150         int conn_array_id;
1151 #endif
1152
1153         assert( connections != NULL );
1154
1155         if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1156
1157 #ifdef SLAP_MULTI_CONN_ARRAY
1158         for ( conn_array_id = 0;
1159                 conn_array_id < NUM_CONNECTION_ARRAY;
1160                 conn_array_id++ )
1161         {
1162                 ldap_pvt_thread_mutex_unlock( &connections_mutex[ conn_array_id ] );
1163         }
1164 #else
1165         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1166 #endif
1167 }
1168
1169 /*
1170  * connection_activity - handle the request operation op on connection
1171  * conn.  This routine figures out what kind of operation it is and
1172  * calls the appropriate stub to handle it.
1173  */
1174
1175 #ifdef SLAPD_MONITOR
1176 /* FIXME: returns 0 in case of failure */
1177 #define INCR_OP_INITIATED(index) \
1178         do { \
1179                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1180                 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1181                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1182         } while (0)
1183 #define INCR_OP_COMPLETED(index) \
1184         do { \
1185                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1186                 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1187                 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1188                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1189         } while (0)
1190 #else /* !SLAPD_MONITOR */
1191 #define INCR_OP_INITIATED(index) do { } while (0)
1192 #define INCR_OP_COMPLETED(index) \
1193         do { \
1194                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1195                 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1196                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1197         } while (0)
1198 #endif /* !SLAPD_MONITOR */
1199
1200 /*
1201  * NOTE: keep in sync with enum in slapd.h
1202  */
1203 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
1204         do_bind,
1205         do_unbind,
1206         do_add,
1207         do_delete,
1208         do_modrdn,
1209         do_modify,
1210         do_compare,
1211         do_search,
1212         do_abandon,
1213         do_extended,
1214         NULL
1215 };
1216
1217 static void *
1218 connection_operation( void *ctx, void *arg_v )
1219 {
1220         int rc = LDAP_OTHER;
1221         Operation *op = arg_v;
1222         SlapReply rs = {REP_RESULT};
1223         ber_tag_t tag = op->o_tag;
1224         int opidx = -1;
1225         Connection *conn = op->o_conn;
1226         void *memctx = NULL;
1227         void *memctx_null = NULL;
1228         ber_len_t memsiz;
1229
1230         ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1231         /* FIXME: returns 0 in case of failure */
1232         ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1233         ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1234
1235         op->o_threadctx = ctx;
1236
1237         switch ( tag ) {
1238         case LDAP_REQ_BIND:
1239         case LDAP_REQ_UNBIND:
1240         case LDAP_REQ_ADD:
1241         case LDAP_REQ_DELETE:
1242         case LDAP_REQ_MODDN:
1243         case LDAP_REQ_MODIFY:
1244         case LDAP_REQ_COMPARE:
1245         case LDAP_REQ_SEARCH:
1246         case LDAP_REQ_ABANDON:
1247         case LDAP_REQ_EXTENDED:
1248                 break;
1249         default:
1250                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1251                         "conn %lu unknown LDAP request 0x%lx\n",
1252                         conn->c_connid, tag, 0 );
1253                 op->o_tag = LBER_ERROR;
1254                 rs.sr_err = LDAP_PROTOCOL_ERROR;
1255                 rs.sr_text = "unknown LDAP request";
1256                 send_ldap_disconnect( op, &rs );
1257                 rc = SLAPD_DISCONNECT;
1258                 goto operations_error;
1259         }
1260
1261         if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1262                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1263                         "error: SASL bind in progress (tag=%ld).\n",
1264                         (long) tag, 0, 0 );
1265                 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1266                         "SASL bind in progress" );
1267                 rc = LDAP_OPERATIONS_ERROR;
1268                 goto operations_error;
1269         }
1270
1271         /* We can use Thread-Local storage for most mallocs. We can
1272          * also use TL for ber parsing, but not on Add or Modify.
1273          */
1274 #if 0
1275         memsiz = ber_len( op->o_ber ) * 64;
1276         if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1277 #endif
1278         memsiz = SLAP_SLAB_SIZE;
1279
1280         memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1281         op->o_tmpmemctx = memctx;
1282         op->o_tmpmfuncs = &slap_sl_mfuncs;
1283         if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1284                 /* Note - the ber and its buffer are already allocated from
1285                  * regular memory; this only affects subsequent mallocs that
1286                  * ber_scanf may invoke.
1287                  */
1288                 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1289         }
1290
1291         switch ( tag ) {
1292         case LDAP_REQ_BIND:
1293                 opidx = SLAP_OP_BIND;
1294                 break;
1295
1296         case LDAP_REQ_UNBIND:
1297                 opidx = SLAP_OP_UNBIND;
1298                 break;
1299
1300         case LDAP_REQ_ADD:
1301                 opidx = SLAP_OP_ADD;
1302                 break;
1303
1304         case LDAP_REQ_DELETE:
1305                 opidx = SLAP_OP_DELETE;
1306                 break;
1307
1308         case LDAP_REQ_MODRDN:
1309                 opidx = SLAP_OP_MODRDN;
1310                 break;
1311
1312         case LDAP_REQ_MODIFY:
1313                 opidx = SLAP_OP_MODIFY;
1314                 break;
1315
1316         case LDAP_REQ_COMPARE:
1317                 opidx = SLAP_OP_COMPARE;
1318                 break;
1319
1320         case LDAP_REQ_SEARCH:
1321                 opidx = SLAP_OP_SEARCH;
1322                 break;
1323
1324         case LDAP_REQ_ABANDON:
1325                 opidx = SLAP_OP_ABANDON;
1326                 break;
1327
1328         case LDAP_REQ_EXTENDED:
1329                 opidx = SLAP_OP_EXTENDED;
1330                 break;
1331
1332         default:
1333                 /* not reachable */
1334                 assert( 0 );
1335         }
1336
1337         assert( opidx > -1 );
1338         INCR_OP_INITIATED( opidx );
1339         rc = (*(opfun[opidx]))( op, &rs );
1340
1341 operations_error:
1342         if ( rc == SLAPD_DISCONNECT ) {
1343                 tag = LBER_ERROR;
1344
1345         } else if ( opidx > -1 ) {
1346                 /* increment completed operations count 
1347                  * only if operation was initiated
1348                  * and rc != SLAPD_DISCONNECT */
1349                 INCR_OP_COMPLETED( opidx );
1350         }
1351
1352         if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1353                 if ( rc == SLAPD_ABANDON ) {
1354                         op->o_cancel = SLAP_CANCEL_ACK;
1355                 } else {
1356                         op->o_cancel = LDAP_TOO_LATE;
1357                 }
1358         }
1359         while ( op->o_cancel != SLAP_CANCEL_NONE &&
1360                 op->o_cancel != SLAP_CANCEL_DONE )
1361         {
1362                 ldap_pvt_thread_yield();
1363         }
1364
1365         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1366
1367         ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1368
1369         LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1370         LDAP_STAILQ_NEXT(op, o_next) = NULL;
1371         slap_op_free( op );
1372         conn->c_n_ops_executing--;
1373         conn->c_n_ops_completed++;
1374
1375         switch( tag ) {
1376         case LBER_ERROR:
1377         case LDAP_REQ_UNBIND:
1378                 /* c_mutex is locked */
1379                 connection_closing( conn,
1380                         tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1381                 break;
1382         }
1383
1384         connection_resched( conn );
1385         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1386         return NULL;
1387 }
1388
1389 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1390
1391 int connection_client_setup(
1392         ber_socket_t s,
1393         ldap_pvt_thread_start_t *func,
1394         void *arg )
1395 {
1396         int rc;
1397         Connection *c;
1398
1399         rc = connection_init( s, (Listener *)&dummy_list, "", "",
1400                 CONN_IS_CLIENT, 0, NULL );
1401         if ( rc < 0 ) return -1;
1402
1403         c = connection_get( s );
1404         c->c_clientfunc = func;
1405         c->c_clientarg = arg;
1406
1407         slapd_add_internal( s, 0 );
1408         slapd_set_read( s, 1 );
1409         connection_return( c );
1410         return 0;
1411 }
1412
1413 void connection_client_enable(
1414         ber_socket_t s )
1415 {
1416         slapd_set_read( s, 1 );
1417 }
1418
1419 void connection_client_stop(
1420         ber_socket_t s )
1421 {
1422         Connection *c;
1423
1424         /* get (locked) connection */
1425         c = connection_get( s );
1426         
1427         assert( c->c_conn_state == SLAP_C_CLIENT );
1428
1429         c->c_listener = NULL;
1430         c->c_conn_state = SLAP_C_INVALID;
1431         c->c_struct_state = SLAP_C_UNUSED;
1432         c->c_close_reason = "?";                        /* should never be needed */
1433         slapd_sd_lock();
1434         ber_sockbuf_free( c->c_sb );
1435         slapd_remove( s, 0, 1, 1 );
1436         c->c_sb = ber_sockbuf_alloc( );
1437         {
1438                 ber_len_t max = sockbuf_max_incoming;
1439                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1440         }
1441
1442         connection_return( c );
1443 }
1444
1445 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1446
1447 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1448
1449 static void* connection_read_thread( void* ctx, void* argv )
1450 {
1451         int rc ;
1452         conn_readinfo cri = { NULL, NULL, NULL, 0 };
1453         ber_socket_t s = (long)argv;
1454
1455         /*
1456          * read incoming LDAP requests. If there is more than one,
1457          * the first one is returned with new_op
1458          */
1459         if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1460                 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1461                 return (void*)(long)rc;
1462         }
1463
1464         /* execute a single queued request in the same thread */
1465         if( cri.op && !cri.nullop ) {
1466                 rc = (long)connection_operation( ctx, cri.op );
1467         } else if ( cri.func ) {
1468                 rc = (long)cri.func( ctx, cri.arg );
1469         }
1470
1471         return (void*)(long)rc;
1472 }
1473
1474 int connection_read_activate( ber_socket_t s )
1475 {
1476         int rc;
1477
1478         /*
1479          * suspend reading on this file descriptor until a connection processing
1480          * thread reads data on it. Otherwise the listener thread will repeatedly
1481          * submit the same event on it to the pool.
1482          */
1483         rc = slapd_clr_read( s, 0 );
1484         if ( rc )
1485                 return rc;
1486
1487         rc = ldap_pvt_thread_pool_submit( &connection_pool,
1488                 connection_read_thread, (void *)(long)s );
1489
1490         if( rc != 0 ) {
1491                 Debug( LDAP_DEBUG_ANY,
1492                         "connection_read_activate(%d): submit failed (%d)\n",
1493                         s, rc, 0 );
1494         }
1495
1496         return rc;
1497 }
1498 #endif
1499
1500 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1501 static int
1502 connection_read( ber_socket_t s, conn_readinfo *cri )
1503 #else
1504 int connection_read(ber_socket_t s)
1505 #endif
1506 {
1507         int rc = 0;
1508         Connection *c;
1509
1510         assert( connections != NULL );
1511
1512         ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
1513
1514         /* get (locked) connection */
1515         c = connection_get( s );
1516
1517         if( c == NULL ) {
1518                 Debug( LDAP_DEBUG_ANY,
1519                         "connection_read(%ld): no connection!\n",
1520                         (long) s, 0, 0 );
1521
1522                 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1523                 return -1;
1524         }
1525
1526         c->c_n_read++;
1527
1528         if( c->c_conn_state == SLAP_C_CLOSING ) {
1529                 Debug( LDAP_DEBUG_TRACE,
1530                         "connection_read(%d): closing, ignoring input for id=%lu\n",
1531                         s, c->c_connid, 0 );
1532
1533 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1534                 slapd_set_read( s, 1 );
1535 #endif
1536                 connection_return( c );
1537                 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1538                 return 0;
1539         }
1540
1541         if ( c->c_conn_state == SLAP_C_CLIENT ) {
1542 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1543                 cri->func = c->c_clientfunc;
1544                 cri->arg = c->c_clientarg;
1545                 /* read should already be cleared */
1546 #else
1547                 slapd_clr_read( s, 0 );
1548                 ldap_pvt_thread_pool_submit( &connection_pool,
1549                         c->c_clientfunc, c->c_clientarg );
1550 #endif
1551                 connection_return( c );
1552                 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1553                 return 0;
1554         }
1555
1556         Debug( LDAP_DEBUG_TRACE,
1557                 "connection_read(%d): checking for input on id=%lu\n",
1558                 s, c->c_connid, 0 );
1559
1560 #ifdef HAVE_TLS
1561         if ( c->c_is_tls && c->c_needs_tls_accept ) {
1562                 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1563                 if ( rc < 0 ) {
1564                         Debug( LDAP_DEBUG_TRACE,
1565                                 "connection_read(%d): TLS accept failure "
1566                                 "error=%d id=%lu, closing\n",
1567                                 s, rc, c->c_connid );
1568
1569                         c->c_needs_tls_accept = 0;
1570                         /* connections_mutex and c_mutex are locked */
1571                         connection_closing( c, "TLS negotiation failure" );
1572
1573 #if 0
1574                         {
1575                                 struct timeval tv;
1576                                 fd_set rfd;
1577                                 /* Drain input before close, to allow SSL error codes
1578                                  * to propagate to client. */
1579                                 FD_ZERO(&rfd);
1580                                 FD_SET(s, &rfd);
1581                                 for (rc=1; rc>0;) {
1582                                         tv.tv_sec = 1;
1583                                         tv.tv_usec = 0;
1584                                         rc = select(s+1, &rfd, NULL, NULL, &tv);
1585                                         if (rc == 1) {
1586                                                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1587                                         }
1588                                 }
1589                         }
1590 #endif
1591
1592                         connection_close( c );
1593                         connection_return( c );
1594                         ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1595                         return 0;
1596
1597                 } else if ( rc == 0 ) {
1598                         void *ssl;
1599                         struct berval authid = BER_BVNULL;
1600
1601                         c->c_needs_tls_accept = 0;
1602
1603                         /* we need to let SASL know */
1604                         ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1605
1606                         c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1607                         if( c->c_tls_ssf > c->c_ssf ) {
1608                                 c->c_ssf = c->c_tls_ssf;
1609                         }
1610
1611                         rc = dnX509peerNormalize( ssl, &authid );
1612                         if ( rc != LDAP_SUCCESS ) {
1613                                 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1614                                         "unable to get TLS client DN, error=%d id=%lu\n",
1615                                         s, rc, c->c_connid );
1616                         }
1617                         Statslog( LDAP_DEBUG_STATS,
1618                                 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1619                             c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1620                         slap_sasl_external( c, c->c_tls_ssf, &authid );
1621                         if ( authid.bv_val ) free( authid.bv_val );
1622                 }
1623
1624                 /* if success and data is ready, fall thru to data input loop */
1625                 if( rc != 0 ||
1626                         !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1627                 {
1628 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1629                         slapd_set_read( s, 1 );
1630 #endif
1631
1632                         connection_return( c );
1633                         ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1634                         return 0;
1635                 }
1636         }
1637 #endif
1638
1639 #ifdef HAVE_CYRUS_SASL
1640         if ( c->c_sasl_layers ) {
1641                 /* If previous layer is not removed yet, give up for now */
1642                 if ( !c->c_sasl_sockctx ) {
1643 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1644                         slapd_set_read( s, 1 );
1645 #endif
1646
1647                         connection_return( c );
1648                         ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1649                         return 0;
1650                 }
1651
1652                 c->c_sasl_layers = 0;
1653
1654                 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1655                 if( rc != LDAP_SUCCESS ) {
1656                         Debug( LDAP_DEBUG_TRACE,
1657                                 "connection_read(%d): SASL install error "
1658                                 "error=%d id=%lu, closing\n",
1659                                 s, rc, c->c_connid );
1660
1661                         /* connections_mutex and c_mutex are locked */
1662                         connection_closing( c, "SASL layer install failure" );
1663                         connection_close( c );
1664                         connection_return( c );
1665                         ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1666                         return 0;
1667                 }
1668         }
1669 #endif
1670
1671 #define CONNECTION_INPUT_LOOP 1
1672 /* #define      DATA_READY_LOOP 1 */
1673
1674         do {
1675                 /* How do we do this without getting into a busy loop ? */
1676 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1677                 rc = connection_input( c, cri );
1678 #else
1679                 rc = connection_input( c );
1680 #endif
1681         }
1682 #ifdef DATA_READY_LOOP
1683         while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1684 #elif CONNECTION_INPUT_LOOP
1685         while(!rc);
1686 #else
1687         while(0);
1688 #endif
1689
1690         if( rc < 0 ) {
1691                 Debug( LDAP_DEBUG_TRACE,
1692                         "connection_read(%d): input error=%d id=%lu, closing.\n",
1693                         s, rc, c->c_connid );
1694
1695                 /* connections_mutex and c_mutex are locked */
1696                 connection_closing( c, conn_lost_str );
1697                 connection_close( c );
1698                 connection_return( c );
1699                 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1700                 return 0;
1701         }
1702
1703 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1704         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1705                 slapd_set_write( s, 0 );
1706         }
1707
1708         slapd_set_read( s, 1 );
1709 #else
1710         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1711                 slapd_set_read( s, 1 );
1712         }
1713
1714         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1715                 slapd_set_write( s, 1 );
1716         }
1717 #endif
1718
1719         connection_return( c );
1720         ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1721
1722         return 0;
1723 }
1724
1725 static int
1726 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1727 connection_input( Connection *conn , conn_readinfo *cri )
1728 #else
1729 connection_input( Connection *conn )
1730 #endif
1731 {
1732         Operation *op;
1733         ber_tag_t       tag;
1734         ber_len_t       len;
1735         ber_int_t       msgid;
1736         BerElement      *ber;
1737         int             rc;
1738 #ifdef LDAP_CONNECTIONLESS
1739         Sockaddr        peeraddr;
1740         char            *cdn = NULL;
1741 #endif
1742         char *defer = NULL;
1743
1744         if ( conn->c_currentber == NULL &&
1745                 ( conn->c_currentber = ber_alloc()) == NULL )
1746         {
1747                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1748                 return -1;
1749         }
1750
1751         errno = 0;
1752
1753 #ifdef LDAP_CONNECTIONLESS
1754         if ( conn->c_is_udp ) {
1755                 char peername[sizeof("IP=255.255.255.255:65336")];
1756
1757                 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1758                 if (len != sizeof(struct sockaddr)) return 1;
1759
1760                 sprintf( peername, "IP=%s:%d",
1761                         inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1762                         (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1763                 Statslog( LDAP_DEBUG_STATS,
1764                         "conn=%lu UDP request from %s (%s) accepted.\n",
1765                         conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1766         }
1767 #endif
1768
1769         tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1770         if ( tag != LDAP_TAG_MESSAGE ) {
1771                 int err = errno;
1772                 ber_socket_t    sd;
1773
1774                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1775
1776                 Debug( LDAP_DEBUG_TRACE,
1777                         "ber_get_next on fd %d failed errno=%d (%s)\n",
1778                         sd, err, sock_errstr(err) );
1779                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1780                         /* log, close and send error */
1781                         ber_free( conn->c_currentber, 1 );
1782                         conn->c_currentber = NULL;
1783
1784                         return -2;
1785                 }
1786                 return 1;
1787         }
1788
1789         ber = conn->c_currentber;
1790         conn->c_currentber = NULL;
1791
1792         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1793                 /* log, close and send error */
1794                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1795                 ber_free( ber, 1 );
1796                 return -1;
1797         }
1798
1799         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1800                 /* log, close and send error */
1801                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1802                 ber_free( ber, 1 );
1803
1804                 return -1;
1805         }
1806
1807 #ifdef LDAP_CONNECTIONLESS
1808         if( conn->c_is_udp ) {
1809                 if( tag == LBER_OCTETSTRING ) {
1810                         ber_get_stringa( ber, &cdn );
1811                         tag = ber_peek_tag(ber, &len);
1812                 }
1813                 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1814                         Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1815                         ber_free( ber, 1 );
1816                         return 0;
1817                 }
1818         }
1819 #endif
1820
1821         if(tag == LDAP_REQ_BIND) {
1822                 /* immediately abandon all existing operations upon BIND */
1823                 connection_abandon( conn );
1824         }
1825
1826         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1827
1828         op->o_conn = conn;
1829         /* clear state if the connection is being reused from inactive */
1830         if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1831                 memset( &conn->c_pagedresults_state, 0,
1832                         sizeof( conn->c_pagedresults_state ) );
1833         }
1834
1835         op->o_res_ber = NULL;
1836
1837 #ifdef LDAP_CONNECTIONLESS
1838         if (conn->c_is_udp) {
1839                 if ( cdn ) {
1840                         ber_str2bv( cdn, 0, 1, &op->o_dn );
1841                         op->o_protocol = LDAP_VERSION2;
1842                 }
1843                 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1844                 if (op->o_res_ber == NULL) return 1;
1845
1846                 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1847                         sizeof(struct sockaddr), 0 );
1848
1849                 if (rc != sizeof(struct sockaddr)) {
1850                         Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1851                         return 1;
1852                 }
1853
1854                 if (op->o_protocol == LDAP_VERSION2) {
1855                         rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1856                         if (rc == -1) {
1857                                 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1858                                 return rc;
1859                         }
1860                 }
1861         }
1862 #endif /* LDAP_CONNECTIONLESS */
1863
1864         rc = 0;
1865
1866         /* Don't process requests when the conn is in the middle of a
1867          * Bind, or if it's closing. Also, don't let any single conn
1868          * use up all the available threads, and don't execute if we're
1869          * currently blocked on output. And don't execute if there are
1870          * already pending ops, let them go first.  Abandon operations
1871          * get exceptions to some, but not all, cases.
1872          */
1873         switch( tag ){
1874         default:
1875                 /* Abandon and Unbind are exempt from these checks */
1876                 if (conn->c_conn_state == SLAP_C_CLOSING) {
1877                         defer = "closing";
1878                         break;
1879                 } else if (conn->c_writewaiter) {
1880                         defer = "awaiting write";
1881                         break;
1882                 } else if (conn->c_n_ops_pending) {
1883                         defer = "pending operations";
1884                         break;
1885                 }
1886                 /* FALLTHRU */
1887         case LDAP_REQ_ABANDON:
1888                 /* Unbind is exempt from these checks */
1889                 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1890                         defer = "too many executing";
1891                         break;
1892                 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1893                         defer = "binding";
1894                         break;
1895                 }
1896                 /* FALLTHRU */
1897         case LDAP_REQ_UNBIND:
1898                 break;
1899         }
1900
1901         if( defer ) {
1902                 int max = conn->c_dn.bv_len
1903                         ? slap_conn_max_pending_auth
1904                         : slap_conn_max_pending;
1905
1906                 Debug( LDAP_DEBUG_ANY,
1907                         "connection_input: conn=%lu deferring operation: %s\n",
1908                         conn->c_connid, defer, 0 );
1909                 conn->c_n_ops_pending++;
1910                 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1911                 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1912
1913         } else {
1914                 conn->c_n_ops_executing++;
1915
1916 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1917                 /*
1918                  * The first op will be processed in the same thread context,
1919                  * as long as there is only one op total.
1920                  * Subsequent ops will be submitted to the pool by
1921                  * calling connection_op_activate()
1922                  */
1923                 if ( cri->op == NULL ) {
1924                         /* the first incoming request */
1925                         connection_op_queue( op );
1926                         cri->op = op;
1927                 } else {
1928                         if ( !cri->nullop ) {
1929                                 cri->nullop = 1;
1930                                 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1931                                         connection_operation, (void *) cri->op );
1932                         }
1933                         connection_op_activate( op );
1934                 }
1935 #else
1936                 connection_op_activate( op );
1937 #endif
1938         }
1939
1940 #ifdef NO_THREADS
1941         if ( conn->c_struct_state != SLAP_C_USED ) {
1942                 /* connection must have got closed underneath us */
1943                 return 1;
1944         }
1945 #endif
1946
1947         assert( conn->c_struct_state == SLAP_C_USED );
1948         return rc;
1949 }
1950
1951 static int
1952 connection_resched( Connection *conn )
1953 {
1954         Operation *op;
1955
1956         if( conn->c_conn_state == SLAP_C_CLOSING ) {
1957                 int rc;
1958                 ber_socket_t    sd;
1959                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1960
1961                 /* use trylock to avoid possible deadlock */
1962                 rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) );
1963
1964                 if( rc ) {
1965                         Debug( LDAP_DEBUG_TRACE,
1966                                 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1967                                 conn->c_connid, sd, 0 );
1968                         /*
1969                          * reaquire locks in the right order...
1970                          * this may allow another thread to close this connection,
1971                          * so recheck state below.
1972                          */
1973                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1974                         ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) );
1975                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1976                 }
1977
1978                 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1979                         Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1980                                 "closed by other thread conn=%lu sd=%d\n",
1981                                 conn->c_connid, sd, 0 );
1982                 } else {
1983                         Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1984                                 "attempting closing conn=%lu sd=%d\n",
1985                                 conn->c_connid, sd, 0 );
1986                         connection_close( conn );
1987                 }
1988
1989                 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) );
1990                 return 0;
1991         }
1992
1993         if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1994                 /* other states need different handling */
1995                 return 0;
1996         }
1997
1998         while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1999                 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
2000
2001                 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
2002                 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2003
2004                 /* pending operations should not be marked for abandonment */
2005                 assert(!op->o_abandon);
2006
2007                 conn->c_n_ops_pending--;
2008                 conn->c_n_ops_executing++;
2009
2010                 connection_op_activate( op );
2011
2012                 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
2013         }
2014         return 0;
2015 }
2016
2017 static void
2018 connection_init_log_prefix( Operation *op )
2019 {
2020         if ( op->o_connid == (unsigned long)(-1) ) {
2021                 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
2022                         "conn=-1 op=%lu", op->o_opid );
2023
2024         } else {
2025                 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
2026                         "conn=%lu op=%lu", op->o_connid, op->o_opid );
2027         }
2028 }
2029
2030 static int connection_bind_cb( Operation *op, SlapReply *rs )
2031 {
2032         slap_callback *cb = op->o_callback;
2033
2034         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
2035         op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2036         op->o_conn->c_sasl_bind_in_progress =
2037                 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
2038
2039         /* Moved here from bind.c due to ITS#4158 */
2040         op->o_conn->c_sasl_bindop = NULL;
2041         if ( op->orb_method == LDAP_AUTH_SASL ) {
2042                 if( rs->sr_err == LDAP_SUCCESS ) {
2043                         ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
2044                         if( !BER_BVISEMPTY( &op->orb_edn ) ) {
2045                                 /* edn is always normalized already */
2046                                 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
2047                         }
2048                         op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
2049                         BER_BVZERO( &op->orb_edn );
2050                         op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
2051                         BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
2052
2053                         op->o_conn->c_sasl_ssf = op->orb_ssf;
2054                         if( op->orb_ssf > op->o_conn->c_ssf ) {
2055                                 op->o_conn->c_ssf = op->orb_ssf;
2056                         }
2057
2058                         if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
2059                                 ber_len_t max = sockbuf_max_incoming_auth;
2060                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
2061                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
2062                         }
2063
2064                         /* log authorization identity */
2065                         Statslog( LDAP_DEBUG_STATS,
2066                                 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
2067                                 op->o_log_prefix,
2068                                 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
2069                                 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
2070
2071                         Debug( LDAP_DEBUG_TRACE,
2072                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
2073                                 op->o_conn->c_authmech.bv_val,
2074                                 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
2075                                 op->orb_ssf );
2076
2077                 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
2078                         if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
2079                                 free( op->o_conn->c_sasl_bind_mech.bv_val );
2080                                 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
2081                         }
2082                 }
2083         }
2084         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
2085
2086         ch_free( op->o_callback );
2087         op->o_callback = NULL;
2088
2089         return SLAP_CB_CONTINUE;
2090 }
2091
2092 static void connection_op_queue( Operation *op )
2093 {
2094         ber_tag_t tag = op->o_tag;
2095
2096         if (tag == LDAP_REQ_BIND) {
2097                 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
2098                 sc->sc_response = connection_bind_cb;
2099                 sc->sc_next = op->o_callback;
2100                 op->o_callback = sc;
2101                 op->o_conn->c_conn_state = SLAP_C_BINDING;
2102         }
2103
2104         if (!op->o_dn.bv_len) {
2105                 op->o_authz = op->o_conn->c_authz;
2106                 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
2107                         ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
2108                         ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
2109                 } else {
2110                         ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
2111                         ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
2112                 }
2113         }
2114
2115         op->o_authtype = op->o_conn->c_authtype;
2116         ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
2117         
2118         if (!op->o_protocol) {
2119                 op->o_protocol = op->o_conn->c_protocol
2120                         ? op->o_conn->c_protocol : LDAP_VERSION3;
2121         }
2122
2123         if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
2124                 op->o_protocol > LDAP_VERSION2)
2125         {
2126                 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2127         }
2128
2129         op->o_connid = op->o_conn->c_connid;
2130         connection_init_log_prefix( op );
2131
2132         LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
2133 }
2134
2135 static int connection_op_activate( Operation *op )
2136 {
2137         int rc;
2138
2139         connection_op_queue( op );
2140
2141         rc = ldap_pvt_thread_pool_submit( &connection_pool,
2142                 connection_operation, (void *) op );
2143
2144         if ( rc != 0 ) {
2145                 Debug( LDAP_DEBUG_ANY,
2146                         "connection_op_activate: submit failed (%d) for conn=%lu\n",
2147                         rc, op->o_connid, 0 );
2148                 /* should move op to pending list */
2149         }
2150
2151         return rc;
2152 }
2153
2154 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2155 static int connection_write( ber_socket_t s );
2156 static void *connection_write_thread( void *ctx, void *arg )
2157 {
2158         return (void *)(long)connection_write((long)arg);
2159 }
2160
2161 int connection_write_activate( ber_socket_t s )
2162 {
2163         int rc;
2164
2165         /*
2166          * suspend reading on this file descriptor until a connection processing
2167          * thread write data on it. Otherwise the listener thread will repeatedly
2168          * submit the same event on it to the pool.
2169          */
2170         slapd_clr_write( s, 0);
2171
2172         rc = ldap_pvt_thread_pool_submit( &connection_pool,
2173                 connection_write_thread, (void *)(long)s );
2174
2175         if( rc != 0 ) {
2176                 Debug( LDAP_DEBUG_ANY,
2177                         "connection_write_activate(%d): submit failed (%d)\n",
2178                         (int) s, rc, 0 );
2179         }
2180         return rc;
2181 }
2182
2183 static
2184 #endif
2185 int connection_write(ber_socket_t s)
2186 {
2187         Connection *c;
2188         Operation *op;
2189
2190         assert( connections != NULL );
2191
2192         ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) );
2193
2194         c = connection_get( s );
2195         if( c == NULL ) {
2196                 Debug( LDAP_DEBUG_ANY,
2197                         "connection_write(%ld): no connection!\n",
2198                         (long)s, 0, 0 );
2199                 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) );
2200                 return -1;
2201         }
2202
2203 #ifndef SLAP_LIGHTWEIGHT_DISPATCHER
2204         slapd_clr_write( s, 0);
2205 #endif
2206         c->c_n_write++;
2207
2208         Debug( LDAP_DEBUG_TRACE,
2209                 "connection_write(%d): waking output for id=%lu\n",
2210                 s, c->c_connid, 0 );
2211         ldap_pvt_thread_cond_signal( &c->c_write_cv );
2212
2213         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
2214                 slapd_set_read( s, 1 );
2215         }
2216         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
2217                 slapd_set_write( s, 1 );
2218         }
2219
2220         /* If there are ops pending because of a writewaiter,
2221          * start one up.
2222          */
2223         while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
2224                 if ( !c->c_writewaiter ) break;
2225                 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
2226
2227                 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
2228                 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2229
2230                 /* pending operations should not be marked for abandonment */
2231                 assert(!op->o_abandon);
2232
2233                 c->c_n_ops_pending--;
2234                 c->c_n_ops_executing++;
2235
2236                 connection_op_activate( op );
2237
2238                 break;
2239         }
2240
2241         connection_return( c );
2242         ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
2243         return 0;
2244 }
2245
2246 void
2247 connection_fake_init(
2248         Connection *conn,
2249         Operation *op,
2250         void *ctx )
2251 {
2252         conn->c_connid = -1;
2253         conn->c_send_ldap_result = slap_send_ldap_result;
2254         conn->c_send_search_entry = slap_send_search_entry;
2255         conn->c_send_search_reference = slap_send_search_reference;
2256         conn->c_listener = (Listener *)&dummy_list;
2257         conn->c_peer_domain = slap_empty_bv;
2258         conn->c_peer_name = slap_empty_bv;
2259
2260         memset(op, 0, OPERATION_BUFFER_SIZE);
2261         op->o_hdr = (Opheader *)(op+1);
2262         op->o_controls = (void **)(op->o_hdr+1);
2263         /* set memory context */
2264         op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
2265         op->o_tmpmfuncs = &slap_sl_mfuncs;
2266         op->o_threadctx = ctx;
2267
2268         op->o_conn = conn;
2269         op->o_connid = op->o_conn->c_connid;
2270         connection_init_log_prefix( op );
2271
2272 #ifdef LDAP_SLAPI
2273         slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2274         slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2275 #endif /* LDAP_SLAPI */
2276
2277         slap_op_time( &op->o_time, &op->o_tincr );
2278 }
2279
2280 void
2281 connection_assign_nextid( Connection *conn )
2282 {
2283         ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2284         conn->c_connid = conn_nextid++;
2285         ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
2286 }