connection.c 36.1 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
 * Copyright 1998-2002 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++) {
Gary Williams's avatar
NT port    
Gary Williams committed
237
			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
238
				assert( connections[i].c_conn_state == SLAP_C_INVALID );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
239
				assert( connections[i].c_sb == 0 );
240
241
242
				break;
			}

243
244
245
			ber_sockbuf_ctrl( connections[i].c_sb,
				LBER_SB_OPT_GET_FD, &sd );

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
416
		c->c_authmech.bv_val = NULL;
		c->c_authmech.bv_len = 0;
417
418
419
420
		c->c_dn.bv_val = NULL;
		c->c_dn.bv_len = 0;
		c->c_ndn.bv_val = NULL;
		c->c_ndn.bv_len = 0;
421
422
		c->c_cdn.bv_val = NULL;
		c->c_cdn.bv_len = 0;
423
		c->c_groups = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
424

425
426
427
428
429
430
431
432
		c->c_listener_url.bv_val = NULL;
		c->c_listener_url.bv_len = 0;
		c->c_peer_domain.bv_val = NULL;
		c->c_peer_domain.bv_len = 0;
		c->c_peer_name.bv_val = NULL;
		c->c_peer_name.bv_len = 0;
		c->c_sock_name.bv_val = NULL;
		c->c_sock_name.bv_len = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
433

434
435
		LDAP_STAILQ_INIT(&c->c_ops);
		LDAP_STAILQ_INIT(&c->c_pending_ops);
436

437
438
		c->c_sasl_bind_mech.bv_val = NULL;
		c->c_sasl_bind_mech.bv_len = 0;
439
		c->c_sasl_context = NULL;
440
		c->c_sasl_extra = NULL;
441

442
443
444
		c->c_sb = ber_sockbuf_alloc( );

		{
445
			ber_len_t max = sockbuf_max_incoming;
446
447
448
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
449
		c->c_currentber = NULL;
450

451
452
453
454
		/* 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 );
455

456
457
		c->c_struct_state = SLAP_C_UNUSED;
	}
458

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

Gary Williams's avatar
NT port    
Gary Williams committed
461
    assert( c->c_struct_state == SLAP_C_UNUSED );
462
463
464
465
    assert( c->c_authmech.bv_val == NULL );
    assert( c->c_dn.bv_val == NULL );
    assert( c->c_ndn.bv_val == NULL );
    assert( c->c_cdn.bv_val == NULL );
466
    assert( c->c_groups == NULL );
467
468
469
470
    assert( c->c_listener_url.bv_val == NULL );
    assert( c->c_peer_domain.bv_val == NULL );
    assert( c->c_peer_name.bv_val == NULL );
    assert( c->c_sock_name.bv_val == NULL );
471
472
    assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
    assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
473
	assert( c->c_sasl_bind_mech.bv_val == NULL );
474
	assert( c->c_sasl_context == NULL );
475
	assert( c->c_sasl_extra == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
476
	assert( c->c_currentber == NULL );
477

478
479
480
481
	ber_str2bv( url, 0, 1, &c->c_listener_url );
	ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
	ber_str2bv( peername, 0, 1, &c->c_peer_name );
	ber_str2bv( sockname, 0, 1, &c->c_sock_name );
482

Gary Williams's avatar
NT port    
Gary Williams committed
483
484
485
486
    c->c_n_ops_received = 0;
    c->c_n_ops_executing = 0;
    c->c_n_ops_pending = 0;
    c->c_n_ops_completed = 0;
487
488
489
490

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

492
493
	/* set to zero until bind, implies LDAP_VERSION3 */
	c->c_protocol = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
494

495
496
497
498
499
500
501
#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
502

503
504
505
506
507
508
509
510
511
512
513
#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 );
514
515
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
		LBER_SBIOD_LEVEL_PROVIDER, NULL );
516
517
518
	} else
#endif
	{
519
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
520
521
522
523
524
	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 );
525
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
526
527
528
529

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
532
533
534
	if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
		c /* non-NULL */ ) < 0 )
	{
535
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
536
537
538
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connection_init: conn %d  set nonblocking failed\n",
			   c->c_connid ));
539
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
540
541
		Debug( LDAP_DEBUG_ANY,
			"connection_init(%d, %s): set nonblocking failed\n",
Pierangelo Masarati's avatar
Pierangelo Masarati committed
542
			s, c->c_peer_name.bv_val, 0 );
543
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
544
	}
545

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

Gary Williams's avatar
NT port    
Gary Williams committed
548
549
    c->c_conn_state = SLAP_C_INACTIVE;
    c->c_struct_state = SLAP_C_USED;
550

551
552
553
	c->c_ssf = c->c_transport_ssf = ssf;
	c->c_tls_ssf = 0;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
554
#ifdef HAVE_TLS
555
    if ( tls_udp_option == 1 ) {
556
557
	    c->c_is_tls = 1;
	    c->c_needs_tls_accept = 1;
558
559
560
    } else {
	    c->c_is_tls = 0;
	    c->c_needs_tls_accept = 0;
561
    }
Kurt Zeilenga's avatar
Kurt Zeilenga committed
562
#endif
563

564
565
	slap_sasl_open( c );
	slap_sasl_external( c, ssf, authid );
566

Gary Williams's avatar
NT port    
Gary Williams committed
567
568
    ldap_pvt_thread_mutex_unlock( &c->c_mutex );
    ldap_pvt_thread_mutex_unlock( &connections_mutex );
569

570
571
    backend_connection_init(c);

Gary Williams's avatar
NT port    
Gary Williams committed
572
    return id;
573
574
}

575
576
577
578
579
void connection2anonymous( Connection *c )
{
    assert( connections != NULL );
    assert( c != NULL );

580
581
582
583
584
	{
		ber_len_t max = sockbuf_max_incoming;
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

585
586
587
	if(c->c_authmech.bv_val != NULL ) {
		free(c->c_authmech.bv_val);
		c->c_authmech.bv_val = NULL;
588
	}
589
	c->c_authmech.bv_len = 0;
590

591
592
593
    if(c->c_dn.bv_val != NULL) {
	free(c->c_dn.bv_val);
	c->c_dn.bv_val = NULL;
594
    }
595
596
597
598
    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;
599
    }
600
    c->c_ndn.bv_len = 0;
601

602
603
604
	if(c->c_cdn.bv_val != NULL) {
		free(c->c_cdn.bv_val);
		c->c_cdn.bv_val = NULL;
605
	}
606
	c->c_cdn.bv_len = 0;
607
608
609

	c->c_authc_backend = NULL;
	c->c_authz_backend = NULL;
610
611
612
613
614
    
    {
	GroupAssertion *g, *n;
	for (g = c->c_groups; g; g=n)
	{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
615
	    n = g->ga_next;
616
617
618
619
620
	    free(g);
	}
	c->c_groups = NULL;
    }

621
622
}

623
624
625
static void
connection_destroy( Connection *c )
{
626
	/* note: connections_mutex should be locked by caller */
627
    ber_socket_t	sd;
628

Gary Williams's avatar
NT port    
Gary Williams committed
629
630
631
632
    assert( connections != NULL );
    assert( c != NULL );
    assert( c->c_struct_state != SLAP_C_UNUSED );
    assert( c->c_conn_state != SLAP_C_INVALID );
633
    assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
634

635
636
    backend_connection_destroy(c);

Gary Williams's avatar
NT port    
Gary Williams committed
637
    c->c_protocol = 0;
638
    c->c_connid = -1;
639

640
    c->c_activitytime = c->c_starttime = 0;
641

642
643
	connection2anonymous( c );

644
645
646
	if(c->c_listener_url.bv_val != NULL) {
		free(c->c_listener_url.bv_val);
		c->c_listener_url.bv_val = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
647
	}
648
	c->c_listener_url.bv_len = 0;
649

650
651
652
	if(c->c_peer_domain.bv_val != NULL) {
		free(c->c_peer_domain.bv_val);
		c->c_peer_domain.bv_val = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
653
	}
654
655
	c->c_peer_domain.bv_len = 0;
	if(c->c_peer_name.bv_val != NULL) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
656
#ifdef LDAP_PF_LOCAL
657
658
659
660
661
		/*
		 * If peer was a domain socket, unlink. Mind you,
		 * they may be un-named. Should we leave this to
		 * the client?
		 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
662
663
664
665
666
		if (strncmp(c->c_peer_name.bv_val, "PATH=", 
					sizeof("PATH=") - 1) == 0) {
			char *path = c->c_peer_name.bv_val 
				+ sizeof("PATH=") - 1;
			if (path[0] != '\0') {
667
668
669
				(void)unlink(path);
			}
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
670
#endif /* LDAP_PF_LOCAL */
671

672
673
		free(c->c_peer_name.bv_val);
		c->c_peer_name.bv_val = NULL;
674
	}
675
676
677
678
	c->c_peer_name.bv_len = 0;
	if(c->c_sock_name.bv_val != NULL) {
		free(c->c_sock_name.bv_val);
		c->c_sock_name.bv_val = NULL;
679
	}
680
	c->c_sock_name.bv_len = 0;
681

682
	c->c_sasl_bind_in_progress = 0;
683
684
685
	if(c->c_sasl_bind_mech.bv_val != NULL) {
		free(c->c_sasl_bind_mech.bv_val);
		c->c_sasl_bind_mech.bv_val = NULL;
686
	}
687
	c->c_sasl_bind_mech.bv_len = 0;
688
689

	slap_sasl_close( c );
690

Kurt Zeilenga's avatar
Kurt Zeilenga committed
691
692
693
694
695
	if ( c->c_currentber != NULL ) {
		ber_free( c->c_currentber, 1 );
		c->c_currentber = NULL;
	}

696
697
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
	if ( sd != AC_SOCKET_INVALID ) {
698
		slapd_remove( sd, 0 );
699
700

		Statslog( LDAP_DEBUG_STATS,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
701
		    "conn=%ld fd=%d closed\n",
702
703
704
			c->c_connid, sd, 0, 0, 0 );
	}

Gary Williams's avatar
Gary Williams committed
705
	ber_sockbuf_free( c->c_sb );
706

707
	c->c_sb = ber_sockbuf_alloc( );
708

709
	{
710
		ber_len_t max = sockbuf_max_incoming;
711
712
713
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

714
715
    c->c_conn_state = SLAP_C_INVALID;
    c->c_struct_state = SLAP_C_UNUSED;
716
717
}

718
719
int connection_state_closing( Connection *c )
{
720
721
	/* c_mutex must be locked by caller */

722
	int state;
723
724
725
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );

726
727
728
729
730
	state = c->c_conn_state;

	assert( state != SLAP_C_INVALID );

	return state == SLAP_C_CLOSING;
731
732
}

733
734
735
736
737
738
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

	Operation *o;

739
	LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
740
741
742
743
744
745
		ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
		o->o_abandon = 1;
		ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
	}

	/* remove pending operations */
746
747
748
	while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
		LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
		LDAP_STAILQ_NEXT(o, o_next) = NULL;
749
750
751
752
		slap_op_free( o );
	}
}

753
754
755
756
757
758
759
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 );

760
761
	/* c_mutex must be locked by caller */

762
	if( c->c_conn_state != SLAP_C_CLOSING ) {
763
764
765
		ber_socket_t	sd;

		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
766
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
767
768
769
		LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
			   "connection_closing: conn %d readying socket %d for close.\n",
			   c->c_connid, sd ));
770
#else
771
		Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
772
			"connection_closing: readying conn=%ld sd=%d for close\n",
773
			c->c_connid, sd, 0 );
774
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
775
776
777
		/* update state to closing */
		c->c_conn_state = SLAP_C_CLOSING;

778
		/* don't listen on this port anymore */
779
		slapd_clr_read( sd, 1 );
780
781

		/* abandon active operations */
782
		connection_abandon( c );
783
784

		/* wake write blocked operations */
785
		slapd_clr_write( sd, 1 );
786
		ldap_pvt_thread_cond_signal( &c->c_write_cv );
787
788
789
	}
}

790
791
static void connection_close( Connection *c )
{
792
793
	ber_socket_t	sd;

794
795
796
797
798
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state == SLAP_C_CLOSING );

799
	/* note: connections_mutex and c_mutex should be locked by caller */
800

801
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
802
	if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
803
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
804
805
806
		LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
			   "connection_close: conn %d  deferring sd %d\n",
			   c->c_connid, sd ));
807
#else
808
		Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
809
			"connection_close: deferring conn=%ld sd=%d\n",
810
			c->c_connid, sd, 0 );
811
#endif
812
813
814
		return;
	}

815
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
816
817
818
	LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
		   "connection_close: conn %d  sd %d\n",
		   c->c_connid, sd ));
819
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
820
	Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
821
		c->c_connid, sd, 0 );
822
#endif
823
824
825
	connection_destroy( c );
}

826
unsigned long connections_nextid(void)
827
{
828
	unsigned long id;
829
830
831
832
833
834
835
836
837
838
839
	assert( connections != NULL );

	ldap_pvt_thread_mutex_lock( &connections_mutex );

	id = conn_nextid;

	ldap_pvt_thread_mutex_unlock( &connections_mutex );

	return id;
}

840
Connection* connection_first( ber_socket_t *index )
841
842
{
	assert( connections != NULL );
843
	assert( index != NULL );
844
845
846

	ldap_pvt_thread_mutex_lock( &connections_mutex );

847
	*index = 0;
848

849
	return connection_next(NULL, index);
850
851
}

852
Connection* connection_next( Connection *c, ber_socket_t *index )
853
854
{
	assert( connections != NULL );
855
856
	assert( index != NULL );
	assert( *index <= dtblsize );
857
858
859
860
861
862
863

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

	c = NULL;

864
865
866
	for(; *index < dtblsize; (*index)++) {
		if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
			assert( connections[*index].c_conn_state == SLAP_C_INVALID );
867
868
869
870
871
872
873
#ifndef HAVE_WINSOCK
			continue;
#else
			break;
#endif
		}

874
875
876
		if( connections[*index].c_struct_state == SLAP_C_USED ) {
			assert( connections[*index].c_conn_state != SLAP_C_INVALID );
			c = &connections[(*index)++];
877
878
879
			break;
		}

880
881
		assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
		assert( connections[*index].c_conn_state == SLAP_C_INVALID );
882
883
884
885
886
887
888
889
890
	}

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

	return c;
}

891
void connection_done( Connection *c )
892
893
894
895
896
897
898
899
900
901
{
	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
902
903
904
905
906
907
/*
 * 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.
 */

908
909
static void *
connection_operation( void *arg_v )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
910
{
911
	int rc;
912
	struct co_arg	*arg = arg_v;
913
	ber_tag_t tag = arg->co_op->o_tag;
914
	Connection *conn = arg->co_conn;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
915

Kurt Zeilenga's avatar
Kurt Zeilenga committed
916
917
918
	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
919

920
	if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
921
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
922
923
924
		LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
			   "connection_operation: conn %d  SASL bind in progress (tag=%ld).\n",
			   conn->c_connid, (long)tag ));
925
#else
926
927
928
		Debug( LDAP_DEBUG_ANY, "connection_operation: "
			"error: SASL bind in progress (tag=%ld).\n",
			(long) tag, 0, 0 );
929
#endif
930
931
		send_ldap_result( conn, arg->co_op,
			rc = LDAP_OPERATIONS_ERROR,
932
933
934
935
			NULL, "SASL bind in progress", NULL, NULL );
		goto operations_error;
	}

936
	switch ( tag ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
937
	case LDAP_REQ_BIND:
938
		rc = do_bind( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
939
940
941
		break;

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

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

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

	case LDAP_REQ_MODRDN: