connection.c 36.8 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
5
6
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
#include "portable.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
8
9

#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
10
#include <limits.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
11

12
#include <ac/socket.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
13
14
15
16
#include <ac/errno.h>
#include <ac/signal.h>
#include <ac/string.h>
#include <ac/time.h>
Pierangelo Masarati's avatar
Pierangelo Masarati committed
17
#include <ac/unistd.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
18

Kurt Zeilenga's avatar
Kurt Zeilenga committed
19
#include "ldap_pvt.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
20
#include "lutil.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
21
22
#include "slap.h"

23
24
25
/* protected by connections_mutex */
static ldap_pvt_thread_mutex_t connections_mutex;
static Connection *connections = NULL;
26
static unsigned long conn_nextid = 0;
27

28
/* structure state (protected by connections_mutex) */
29
30
31
#define SLAP_C_UNINITIALIZED	0x00	/* MUST BE ZERO (0) */
#define SLAP_C_UNUSED			0x01
#define SLAP_C_USED				0x02
32
33

/* connection state (protected by c_mutex ) */
34
35
36
37
38
#define SLAP_C_INVALID			0x00	/* MUST BE ZERO (0) */
#define SLAP_C_INACTIVE			0x01	/* zero threads */
#define SLAP_C_ACTIVE			0x02	/* one or more threads */
#define SLAP_C_BINDING			0x03	/* binding */
#define SLAP_C_CLOSING			0x04	/* closing */
39

40
41
42
const char *
connection_state2str( int state )
{
43
44
45
46
47
48
49
50
51
52
53
	switch( state ) {
	case SLAP_C_INVALID:	return "!";		
	case SLAP_C_INACTIVE:	return "|";		
	case SLAP_C_ACTIVE:		return "";			
	case SLAP_C_BINDING:	return "B";
	case SLAP_C_CLOSING:	return "C";			
	}

	return "?";
}

54
static Connection* connection_get( ber_socket_t s );
55
56

static int connection_input( Connection *c );
57
static void connection_close( Connection *c );
58

59
60
static int connection_op_activate( Connection *conn, Operation *op );
static int connection_resched( Connection *conn );
61
static void connection_abandon( Connection *conn );
62
static void connection_destroy( Connection *c );
63

Kurt Zeilenga's avatar
Kurt Zeilenga committed
64
65
66
67
68
struct co_arg {
	Connection	*co_conn;
	Operation	*co_op;
};

69
70
71
72
73
74
75
/*
 * Initialize connection management infrastructure.
 */
int connections_init(void)
{
	assert( connections == NULL );

76
	if( connections != NULL) {
77
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
78
79
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connections_init:  already initialized.\n" ));
80
#else
81
82
		Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
			0, 0, 0 );
83
#endif
84
85
86
87
88
89
90
91
92
		return -1;
	}

	/* should check return of every call */
	ldap_pvt_thread_mutex_init( &connections_mutex );

	connections = (Connection *) calloc( dtblsize, sizeof(Connection) );

	if( connections == NULL ) {
93
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
94
95
96
		LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
			   "connections_init: allocation (%d * %ld) of connection array failed\n",
			   dtblsize, (long) sizeof(Connection) ));
97
#else
98
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
99
			"connections_init: allocation (%d*%ld) of connection array failed\n",
100
			dtblsize, (long) sizeof(Connection), 0 );
101
#endif
102
103
104
		return -1;
	}

105
106
107
    assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
    assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );

108
109
110
111
112
113
114
115
	/*
	 * per entry initialization of the Connection array initialization
	 * will be done by connection_init()
	 */ 

	return 0;
}

116
117
118
119
120
/*
 * Destroy connection management infrastructure.
 */
int connections_destroy(void)
{
121
	ber_socket_t i;
122
123
124
125

	/* should check return of every call */

	if( connections == NULL) {
126
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
127
128
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connections_destroy: nothing to destroy.\n"));
129
#else
130
131
		Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
			0, 0, 0 );
132
#endif
133
134
135
136
		return -1;
	}

	for ( i = 0; i < dtblsize; i++ ) {
137
		if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
138
			ber_sockbuf_free( connections[i].c_sb );
139
140
141
142
			ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
			ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
			ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
		}
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	}

	free( connections );
	connections = NULL;

	ldap_pvt_thread_mutex_destroy( &connections_mutex );
	return 0;
}

/*
 * shutdown all connections
 */
int connections_shutdown(void)
{
157
	ber_socket_t i;
158
159
160
161
162
163
164
165
166

	ldap_pvt_thread_mutex_lock( &connections_mutex );

	for ( i = 0; i < dtblsize; i++ ) {
		if( connections[i].c_struct_state != SLAP_C_USED ) {
			continue;
		}

		ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
167
168

		/* connections_mutex and c_mutex are locked */
169
170
		connection_closing( &connections[i] );
		connection_close( &connections[i] );
171

172
173
174
175
		ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
	}

	ldap_pvt_thread_mutex_unlock( &connections_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
176
177

	return 0;
178
179
}

180
181
182
183
184
185
186
187
188
/*
 * Timeout idle connections.
 */
int connections_timeout_idle(time_t now)
{
	int i = 0;
	int connindex;
	Connection* c;

Gary Williams's avatar
Gary Williams committed
189
	for( c = connection_first( &connindex );
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
		c != NULL;
		c = connection_next( c, &connindex ) )
	{
		if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
			/* close it */
			connection_closing( c );
			connection_close( c );
			i++;
		}
	}
	connection_done( c );

	return i;
}

205
static Connection* connection_get( ber_socket_t s )
206
{
207
208
	/* connections_mutex should be locked by caller */

209
210
	Connection *c;

211
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
212
213
	LDAP_LOG(( "connection", LDAP_LEVEL_ENTRY,
		   "connection_get: socket %ld\n", (long)s ));
214
#else
215
	Debug( LDAP_DEBUG_ARGS,
216
217
		"connection_get(%ld)\n",
		(long) s, 0, 0 );
218
#endif
219
220
221

	assert( connections != NULL );

222
	if(s == AC_SOCKET_INVALID) {
223
224
225
226
227
		return NULL;
	}

#ifndef HAVE_WINSOCK
	c = &connections[s];
228

Kurt Zeilenga's avatar
Kurt Zeilenga committed
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
238
239
			ber_sockbuf_ctrl( connections[i].c_sb,
				LBER_SB_OPT_GET_FD, &sd );

Gary Williams's avatar
NT port    
Gary Williams committed
240
			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
241
				assert( connections[i].c_conn_state == SLAP_C_INVALID );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
242
				assert( connections[i].c_sb == 0 );
243
244
245
				break;
			}

Gary Williams's avatar
NT port    
Gary Williams committed
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
250
251
				continue;
			}

252
253
254
			/* state can actually change from used -> unused by resched,
			 * so don't assert details here.
			 */
255

256
			if( sd == s ) {
Gary Williams's avatar
NT port    
Gary Williams committed
257
				c = &connections[i];
258
259
260
261
262
				break;
			}
		}
	}
#endif
263

264
	if( c != NULL ) {
265
266
		ber_socket_t	sd;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
267
268
		ldap_pvt_thread_mutex_lock( &c->c_mutex );

269
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
270
271
		if( c->c_struct_state != SLAP_C_USED ) {
			/* 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
Gary Williams's avatar
Gary Williams committed
277
278
			LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
				   "connection_get:  connection %d not used\n", s ));
279
#else
280
			Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
281
282
				"connection_get(%d): connection not used\n",
				s, 0, 0 );
283
#endif
284

285
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
286
287
288
			return NULL;
		}

289
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
290
291
		LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
			   "connection_get: get for %d got connid %ld\n",s, c->c_connid ));
292
#else
293
294
295
		Debug( LDAP_DEBUG_TRACE,
			"connection_get(%d): got connid=%ld\n",
			s, c->c_connid, 0 );
296
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
297

298
299
		c->c_n_get++;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
300
301
		assert( c->c_struct_state == SLAP_C_USED );
		assert( c->c_conn_state != SLAP_C_INVALID );
302
		assert( sd != AC_SOCKET_INVALID );
303

304
305
306
307
308
309
310
#ifdef SLAPD_MONITOR
		c->c_activitytime = slap_get_time();
#else
		if( global_idletimeout > 0 ) {
			c->c_activitytime = slap_get_time();
		}
#endif
311
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
312

313
314
315
316
317
318
319
320
321
	return c;
}

static void connection_return( Connection *c )
{
	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
}

long connection_init(
322
	ber_socket_t s,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
323
324
325
326
	const char* url,
	const char* dnsname,
	const char* peername,
	const char* sockname,
327
	int tls_udp_option,
328
	slap_ssf_t ssf,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
329
	const char *authid )
330
{
331
	unsigned long id;
332
	Connection *c;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
333

334
335
	assert( connections != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
336
337
338
339
	assert( dnsname != NULL );
	assert( peername != NULL );
	assert( sockname != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
340
#ifndef HAVE_TLS
341
	assert( tls_udp_option != 1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
342
343
#endif

344
	if( s == AC_SOCKET_INVALID ) {
345
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
346
347
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connection_init: init of socket %ld invalid.\n", (long)s ));
348
#else
Gary Williams's avatar
Gary Williams committed
349
350
351
		Debug( LDAP_DEBUG_ANY,
		       "connection_init(%ld): invalid.\n",
		       (long) s, 0, 0 );
352
#endif
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
		return -1;
	}

	assert( s >= 0 );
#ifndef HAVE_WINSOCK
	assert( s < dtblsize );
#endif

	ldap_pvt_thread_mutex_lock( &connections_mutex );

#ifndef HAVE_WINSOCK
	c = &connections[s];

#else
	{
Howard Chu's avatar
Howard Chu committed
368
		ber_socket_t	i;
369

370
371
		c = NULL;

Gary Williams's avatar
Gary Williams committed
372
373
	for( i=0; i < dtblsize; i++) {
		ber_socket_t	sd;
374

Gary Williams's avatar
Gary Williams committed
375
376
377
378
379
	    if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
		assert( connections[i].c_sb == 0 );
		c = &connections[i];
		break;
	    }
Gary Williams's avatar
NT port    
Gary Williams committed
380

381
382
			sd = AC_SOCKET_INVALID;
			if (connections[i].c_sb != NULL)
Gary Williams's avatar
Gary Williams committed
383
			ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
384
	    
Gary Williams's avatar
Gary Williams committed
385
386
387
388
389
390
391
392
393
394
395
396
	    if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
		assert( sd == AC_SOCKET_INVALID );
		c = &connections[i];
		break;
	    }

	    assert( connections[i].c_struct_state == SLAP_C_USED );
	    assert( connections[i].c_conn_state != SLAP_C_INVALID );
	    assert( sd != AC_SOCKET_INVALID );
	}

	if( c == NULL ) {
397
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
398
399
400
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connection_init: skt %d	 connection table full (%d/%d)\n",
			   s, i, dtblsize ));
401
#else
Gary Williams's avatar
Gary Williams committed
402
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
403
				"connection_init(%d): connection table full (%d/%d)\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
404
				s, i, dtblsize);
405
#endif
Gary Williams's avatar
Gary Williams committed
406
407
408
	    ldap_pvt_thread_mutex_unlock( &connections_mutex );
	    return -1;
	}
Gary Williams's avatar
NT port    
Gary Williams committed
409
    }
410
411
#endif

Gary Williams's avatar
NT port    
Gary Williams committed
412
    assert( c != NULL );
413

414
	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
415
		c->c_authmech = NULL;
416
417
418
419
		c->c_dn.bv_val = NULL;
		c->c_dn.bv_len = 0;
		c->c_ndn.bv_val = NULL;
		c->c_ndn.bv_len = 0;
420
		c->c_cdn = NULL;
421
		c->c_groups = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
422
423
424

		c->c_listener_url = NULL;
		c->c_peer_domain = NULL;
425
426
		c->c_peer_name = NULL;
		c->c_sock_name = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
427

428
429
		STAILQ_INIT(&c->c_ops);
		STAILQ_INIT(&c->c_pending_ops);
430

431
		c->c_sasl_bind_mech = NULL;
432
		c->c_sasl_context = NULL;
433
		c->c_sasl_extra = NULL;
434

435
436
437
		c->c_sb = ber_sockbuf_alloc( );

		{
438
			ber_len_t max = sockbuf_max_incoming;
439
440
441
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
442
		c->c_currentber = NULL;
443

444
445
446
447
		/* should check status of thread calls */
		ldap_pvt_thread_mutex_init( &c->c_mutex );
		ldap_pvt_thread_mutex_init( &c->c_write_mutex );
		ldap_pvt_thread_cond_init( &c->c_write_cv );
448

449
450
		c->c_struct_state = SLAP_C_UNUSED;
	}
451

Gary Williams's avatar
NT port    
Gary Williams committed
452
    ldap_pvt_thread_mutex_lock( &c->c_mutex );
453

Gary Williams's avatar
NT port    
Gary Williams committed
454
    assert( c->c_struct_state == SLAP_C_UNUSED );
455
	assert( c->c_authmech == NULL );
456
457
    assert(	c->c_dn.bv_val == NULL );
    assert(	c->c_ndn.bv_val == NULL );
Gary Williams's avatar
NT port    
Gary Williams committed
458
    assert(	c->c_cdn == NULL );
459
    assert( c->c_groups == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
460
461
462
463
    assert( c->c_listener_url == NULL );
    assert( c->c_peer_domain == NULL );
    assert( c->c_peer_name == NULL );
    assert( c->c_sock_name == NULL );
464
465
    assert( STAILQ_EMPTY(&c->c_ops) );
    assert( STAILQ_EMPTY(&c->c_pending_ops) );
466
	assert( c->c_sasl_bind_mech == NULL );
467
	assert( c->c_sasl_context == NULL );
468
	assert( c->c_sasl_extra == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
469
	assert( c->c_currentber == NULL );
470

Kurt Zeilenga's avatar
Kurt Zeilenga committed
471
472
473
474
	c->c_listener_url = ch_strdup( url  );
	c->c_peer_domain = ch_strdup( dnsname  );
    c->c_peer_name = ch_strdup( peername  );
    c->c_sock_name = ch_strdup( sockname );
475

Gary Williams's avatar
NT port    
Gary Williams committed
476
477
478
479
    c->c_n_ops_received = 0;
    c->c_n_ops_executing = 0;
    c->c_n_ops_pending = 0;
    c->c_n_ops_completed = 0;
480
481
482
483

	c->c_n_get = 0;
	c->c_n_read = 0;
	c->c_n_write = 0;
484

485
486
	/* set to zero until bind, implies LDAP_VERSION3 */
	c->c_protocol = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
487

488
489
490
491
492
493
494
#ifdef SLAPD_MONITOR
	c->c_activitytime = c->c_starttime = slap_get_time();
#else
	if( global_idletimeout > 0 ) {
		c->c_activitytime = c->c_starttime = slap_get_time();
	}
#endif
495

496
497
498
499
500
501
502
503
504
505
506
507
508
509
#ifdef LDAP_CONNECTIONLESS
	c->c_is_udp = 0;
	if (tls_udp_option == 2)
	{
		c->c_is_udp = 1;
#ifdef LDAP_DEBUG
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
		LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
#endif
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
		LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
	} else
#endif
	{
510
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
511
512
513
514
515
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
		LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
#endif
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
		LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
516
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
517
518
519
520
521
522
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
		LBER_SBIOD_LEVEL_PROVIDER, NULL );

#ifdef LDAP_DEBUG
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
		INT_MAX, (void*)"ldap_" );
523
#endif
524

Kurt Zeilenga's avatar
Kurt Zeilenga committed
525
526
527
	if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
		c /* non-NULL */ ) < 0 )
	{
528
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
529
530
531
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connection_init: conn %d  set nonblocking failed\n",
			   c->c_connid ));
532
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
533
534
535
		Debug( LDAP_DEBUG_ANY,
			"connection_init(%d, %s): set nonblocking failed\n",
			s, c->c_peer_name, 0 );
536
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
537
	}
538

Gary Williams's avatar
NT port    
Gary Williams committed
539
    id = c->c_connid = conn_nextid++;
540

Gary Williams's avatar
NT port    
Gary Williams committed
541
542
    c->c_conn_state = SLAP_C_INACTIVE;
    c->c_struct_state = SLAP_C_USED;
543

544
545
546
	c->c_ssf = c->c_transport_ssf = ssf;
	c->c_tls_ssf = 0;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
547
#ifdef HAVE_TLS
548
    if ( tls_udp_option == 1 ) {
549
550
	    c->c_is_tls = 1;
	    c->c_needs_tls_accept = 1;
551
552
553
    } else {
	    c->c_is_tls = 0;
	    c->c_needs_tls_accept = 0;
554
    }
Kurt Zeilenga's avatar
Kurt Zeilenga committed
555
#endif
556

557
558
	slap_sasl_open( c );
	slap_sasl_external( c, ssf, authid );
559

Gary Williams's avatar
NT port    
Gary Williams committed
560
561
    ldap_pvt_thread_mutex_unlock( &c->c_mutex );
    ldap_pvt_thread_mutex_unlock( &connections_mutex );
562

563
564
    backend_connection_init(c);

Gary Williams's avatar
NT port    
Gary Williams committed
565
    return id;
566
567
}

568
569
570
571
572
void connection2anonymous( Connection *c )
{
    assert( connections != NULL );
    assert( c != NULL );

573
574
575
576
577
	{
		ber_len_t max = sockbuf_max_incoming;
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

578
579
580
581
582
	if(c->c_authmech != NULL ) {
		free(c->c_authmech);
		c->c_authmech = NULL;
	}

583
584
585
    if(c->c_dn.bv_val != NULL) {
	free(c->c_dn.bv_val);
	c->c_dn.bv_val = NULL;
586
    }
587
588
589
590
    c->c_dn.bv_len = 0;
    if(c->c_ndn.bv_val != NULL) {
	free(c->c_ndn.bv_val);
	c->c_ndn.bv_val = NULL;
591
    }
592
    c->c_ndn.bv_len = 0;
593
594
595
596
597
598
599
600

	if(c->c_cdn != NULL) {
		free(c->c_cdn);
		c->c_cdn = NULL;
	}

	c->c_authc_backend = NULL;
	c->c_authz_backend = NULL;
601
602
603
604
605
606
607
608
609
610
611
    
    {
	GroupAssertion *g, *n;
	for (g = c->c_groups; g; g=n)
	{
	    n = g->next;
	    free(g);
	}
	c->c_groups = NULL;
    }

612
613
}

614
615
616
static void
connection_destroy( Connection *c )
{
617
	/* note: connections_mutex should be locked by caller */
618
    ber_socket_t	sd;
619

Gary Williams's avatar
NT port    
Gary Williams committed
620
621
622
623
    assert( connections != NULL );
    assert( c != NULL );
    assert( c->c_struct_state != SLAP_C_UNUSED );
    assert( c->c_conn_state != SLAP_C_INVALID );
624
    assert( STAILQ_EMPTY(&c->c_ops) );
625

626
627
    backend_connection_destroy(c);

Gary Williams's avatar
NT port    
Gary Williams committed
628
    c->c_protocol = 0;
629
    c->c_connid = -1;
630

631
    c->c_activitytime = c->c_starttime = 0;
632

633
634
	connection2anonymous( c );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
635
636
637
638
	if(c->c_listener_url != NULL) {
		free(c->c_listener_url);
		c->c_listener_url = NULL;
	}
639

Kurt Zeilenga's avatar
Kurt Zeilenga committed
640
641
642
643
644
	if(c->c_peer_domain != NULL) {
		free(c->c_peer_domain);
		c->c_peer_domain = NULL;
	}
	if(c->c_peer_name != NULL) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
645
#ifdef LDAP_PF_lOCAL
646
647
648
649
650
651
652
653
654
655
656
		/*
		 * If peer was a domain socket, unlink. Mind you,
		 * they may be un-named. Should we leave this to
		 * the client?
		 */
		if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
			char *path = c->c_peer_name + 5;
			if (path != '\0') {
				(void)unlink(path);
			}
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
657
#endif /* LDAP_PF_LOCAL */
658

Kurt Zeilenga's avatar
Kurt Zeilenga committed
659
660
		free(c->c_peer_name);
		c->c_peer_name = NULL;
661
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
662
663
664
	if(c->c_sock_name != NULL) {
		free(c->c_sock_name);
		c->c_sock_name = NULL;
665
666
	}

667
668
669
670
671
	c->c_sasl_bind_in_progress = 0;
	if(c->c_sasl_bind_mech != NULL) {
		free(c->c_sasl_bind_mech);
		c->c_sasl_bind_mech = NULL;
	}
672
673

	slap_sasl_close( c );
674

Kurt Zeilenga's avatar
Kurt Zeilenga committed
675
676
677
678
679
	if ( c->c_currentber != NULL ) {
		ber_free( c->c_currentber, 1 );
		c->c_currentber = NULL;
	}

680
681
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
	if ( sd != AC_SOCKET_INVALID ) {
682
		slapd_remove( sd, 0 );
683
684

		Statslog( LDAP_DEBUG_STATS,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
685
		    "conn=%ld fd=%d closed\n",
686
687
688
			c->c_connid, sd, 0, 0, 0 );
	}

Gary Williams's avatar
Gary Williams committed
689
	ber_sockbuf_free( c->c_sb );
690

691
	c->c_sb = ber_sockbuf_alloc( );
692

693
	{
694
		ber_len_t max = sockbuf_max_incoming;
695
696
697
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

698
699
    c->c_conn_state = SLAP_C_INVALID;
    c->c_struct_state = SLAP_C_UNUSED;
700
701
}

702
703
int connection_state_closing( Connection *c )
{
704
705
	/* c_mutex must be locked by caller */

706
	int state;
707
708
709
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );

710
711
712
713
714
	state = c->c_conn_state;

	assert( state != SLAP_C_INVALID );

	return state == SLAP_C_CLOSING;
715
716
}

717
718
719
720
721
722
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

	Operation *o;

723
	STAILQ_FOREACH(o, &c->c_ops, o_next) {
724
725
726
727
728
729
		ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
		o->o_abandon = 1;
		ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
	}

	/* remove pending operations */
730
731
732
	while ( (o = STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
		STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
		STAILQ_NEXT(o, o_next) = NULL;
733
734
735
736
		slap_op_free( o );
	}
}

737
738
739
740
741
742
743
void connection_closing( Connection *c )
{
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state != SLAP_C_INVALID );

744
745
	/* c_mutex must be locked by caller */

746
	if( c->c_conn_state != SLAP_C_CLOSING ) {
747
748
749
		ber_socket_t	sd;

		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
750
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
751
752
753
		LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
			   "connection_closing: conn %d readying socket %d for close.\n",
			   c->c_connid, sd ));
754
#else
755
		Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
756
			"connection_closing: readying conn=%ld sd=%d for close\n",
757
			c->c_connid, sd, 0 );
758
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
759
760
761
		/* update state to closing */
		c->c_conn_state = SLAP_C_CLOSING;

762
		/* don't listen on this port anymore */
763
		slapd_clr_read( sd, 1 );
764
765

		/* abandon active operations */
766
		connection_abandon( c );
767
768

		/* wake write blocked operations */
769
		slapd_clr_write( sd, 1 );
770
		ldap_pvt_thread_cond_signal( &c->c_write_cv );
771
772
773
	}
}

774
775
static void connection_close( Connection *c )
{
776
777
	ber_socket_t	sd;

778
779
780
781
782
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state == SLAP_C_CLOSING );

783
	/* note: connections_mutex and c_mutex should be locked by caller */
784

785
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
786
	if( !STAILQ_EMPTY(&c->c_ops) ) {
787
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
788
789
790
		LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
			   "connection_close: conn %d  deferring sd %d\n",
			   c->c_connid, sd ));
791
#else
792
		Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
793
			"connection_close: deferring conn=%ld sd=%d\n",
794
			c->c_connid, sd, 0 );
795
#endif
796
797
798
		return;
	}

799
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
800
801
802
	LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
		   "connection_close: conn %d  sd %d\n",
		   c->c_connid, sd ));
803
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
804
	Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
805
		c->c_connid, sd, 0 );
806
#endif
807
808
809
	connection_destroy( c );
}

810
unsigned long connections_nextid(void)
811
{
812
	unsigned long id;
813
814
815
816
817
818
819
820
821
822
823
	assert( connections != NULL );

	ldap_pvt_thread_mutex_lock( &connections_mutex );

	id = conn_nextid;

	ldap_pvt_thread_mutex_unlock( &connections_mutex );

	return id;
}

824
Connection* connection_first( ber_socket_t *index )
825
826
{
	assert( connections != NULL );
827
	assert( index != NULL );
828
829
830

	ldap_pvt_thread_mutex_lock( &connections_mutex );

831
	*index = 0;
832

833
	return connection_next(NULL, index);
834
835
}

836
Connection* connection_next( Connection *c, ber_socket_t *index )
837
838
{
	assert( connections != NULL );
839
840
	assert( index != NULL );
	assert( *index <= dtblsize );
841
842
843
844
845
846
847

	if( c != NULL ) {
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );
	}

	c = NULL;

848
849
850
	for(; *index < dtblsize; (*index)++) {
		if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
			assert( connections[*index].c_conn_state == SLAP_C_INVALID );
851
852
853
854
855
856
857
#ifndef HAVE_WINSOCK
			continue;
#else
			break;
#endif
		}

858
859
860
		if( connections[*index].c_struct_state == SLAP_C_USED ) {
			assert( connections[*index].c_conn_state != SLAP_C_INVALID );
			c = &connections[(*index)++];
861
862
863
			break;
		}

864
865
		assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
		assert( connections[*index].c_conn_state == SLAP_C_INVALID );
866
867
868
869
870
871
872
873
874
	}

	if( c != NULL ) {
		ldap_pvt_thread_mutex_lock( &c->c_mutex );
	}

	return c;
}

875
void connection_done( Connection *c )
876
877
878
879
880
881
882
883
884
885
{
	assert( connections != NULL );

	if( c != NULL ) {
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );
	}

	ldap_pvt_thread_mutex_unlock( &connections_mutex );
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
886
887
888
889
890
891
/*
 * connection_activity - handle the request operation op on connection
 * conn.  This routine figures out what kind of operation it is and
 * calls the appropriate stub to handle it.
 */

892
893
static void *
connection_operation( void *arg_v )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
894
{
895
	int rc;
896
	struct co_arg	*arg = arg_v;
897
	ber_tag_t tag = arg->co_op->o_tag;
898
	Connection *conn = arg->co_conn;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
899

Kurt Zeilenga's avatar
Kurt Zeilenga committed
900
901
902
	ldap_pvt_thread_mutex_lock( &num_ops_mutex );
	num_ops_initiated++;
	ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
903

904
	if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
905
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
906
907
908
		LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
			   "connection_operation: conn %d  SASL bind in progress (tag=%ld).\n",
			   conn->c_connid, (long)tag ));
909
#else
910
911
912
		Debug( LDAP_DEBUG_ANY, "connection_operation: "
			"error: SASL bind in progress (tag=%ld).\n",
			(long) tag, 0, 0 );
913
#endif
914
915
		send_ldap_result( conn, arg->co_op,
			rc = LDAP_OPERATIONS_ERROR,
916
917
918
919
			NULL, "SASL bind in progress", NULL, NULL );
		goto operations_error;
	}

920
	switch ( tag ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
921
	case LDAP_REQ_BIND:
922
		rc = do_bind( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
923
924
925
		break;

	case LDAP_REQ_UNBIND:
926
		rc = do_unbind( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
927
928
929
		break;

	case LDAP_REQ_ADD:
930
		rc = do_add( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
931
932
933
		break;

	case LDAP_REQ_DELETE:
934
		rc = do_delete( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
935
936
937
		break;

	case LDAP_REQ_MODRDN:
938
		rc = do_modrdn( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
939
940
941
		break;

	case LDAP_REQ_MODIFY:
942
		rc = do_modify( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
943
944
945
		break;

	case LDAP_REQ_COMPARE:
946
		rc = do_compare( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
947
948
949
		break;

	case LDAP_REQ_SEARCH:
950
		rc = do_search( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
951
952
953
		break;

	case LDAP_REQ_ABANDON:
954
		rc = do_abandon( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
955
956
		break;

957
	case LDAP_REQ_EXTENDED:
958
		rc = do_extended( conn, arg->co_op );
959
960
		break;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
961
	default:
962
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
963
964
965
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connection_operation: conn %d  unknown LDAP request 0x%lx\n",
			   conn->c_connid, tag ));
966
#else
967
968
		Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
		    tag, 0, 0 );
969
#endif
970
971
972
973
		arg->co_op->o_tag = LBER_ERROR;
		send_ldap_disconnect( conn, arg->co_op,
			LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
		rc = -1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
974
975
976
		break;
	}